Bonjour à tous,

J'ai crée un service Windows qui surveille un répertoire et qui écrit chaque changement dans un fichier texte. Je souhaiterai lui ajouter une fonction qui copie le fichier modifié et qui le colle dans un autre dossier. Est-ce possible ?
Que faut-il faire à partir de mon programme ?


Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Collections;
using System.Xml;
 
namespace WatchDogFolder
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {           
 
            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = @"D:/FTP";
            fsw.Filter = "*.*";
            fsw.EnableRaisingEvents = true;
            fsw.IncludeSubdirectories = true;
            fsw.NotifyFilter = NotifyFilters.LastAccess
                              | NotifyFilters.LastWrite
                              | NotifyFilters.FileName
                              | NotifyFilters.DirectoryName;                       
 
            fsw.Created += new FileSystemEventHandler(fsw_Created);
            fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
            fsw.Changed += new FileSystemEventHandler(fsw_Changed);
            fsw.Renamed += new RenamedEventHandler(fsw_Renamed);            
 
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Démarrage de WatchDogFolder : " + DateTime.Now.ToLongTimeString());
            sw.Close();
        }
 
        protected override void OnStop()
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Arrêt de WatchDogFolder : " + DateTime.Now.ToLongTimeString());
            sw.Close();
        }
 
        protected override void OnContinue()
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Reprise de WatchDogFolder : " + DateTime.Now.ToLongTimeString());
            sw.Close();
        }
 
        protected override void OnPause()
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log.log", true);
            sw.WriteLine("Mise en pause de WatchDogFolder : " + DateTime.Now.ToLongTimeString());
            sw.Close();
        }
 
        protected override void OnShutdown()
        {
            base.OnShutdown();
        }
 
        public static void fsw_Created(Object source, FileSystemEventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Creation de fichier" + e.Name);
            sw.Close();
 
        }
 
        public static void fsw_Deleted(Object source, FileSystemEventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Supression de fichiers" +e.Name);
            sw.Close();
        }
 
        public static void fsw_Changed(Object source, FileSystemEventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Modification de fichiers" +e.Name);
            sw.Close();
        }
 
        public static void fsw_Renamed(Object source, RenamedEventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Fichier renommé"+ e.OldName + "en" + e.Name);
            sw.Close();
 
        }
    }
}

J'ai essayé de repartir avec Source et e des quatres dernières méthodes, mais CopyTo()ne m'est pas proposé.

Auriez-vous des idées ?

Cordialement

Julien