Bonjour à tous,

Je travaille sur un service Windows, qui surveille un dossier et au moindre changement de ce dossier appelle une fonction. (Par exemple pour la création d'un fichier, le renommage la copie etc...). Lors de la création d'un fichier la fonction appelée le transfère sur un serveur FTP. Et lors de la suppression normalement le code devrait envoyer une requête au serveur pour supprimer le fichier en question, mais malheureusement c'est ici que ça bloque ! Lors de l'appel de la fonction de suppression rien ne se passe côté serveur. Je reçois juste un message d'alerte disant que le fichier est déjà utilisé par mon service Windows lorsque je veux le supprimer sur le dossier.

Voici le code :

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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;
using System.Net;
 
namespace WatchDogFolder
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {           
 
            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = @"D:/Test";
            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 + " " + e.FullPath);
            sw.Close();
            string Path = e.FullPath;
            string[] chemin = Path.Split('\\');
            string fin = chemin[0] + '/' + e.Name;
 
            //string Nom = e.Name;
            //string CheminCopy = ("@D:/COPIE/copie3.txt");
 
            //File.Copy(fin, CheminCopy, true);
            UpLoadFile("ftp://172.16.212.113/Images", "Julien", "Julien", fin); 
        }
 
        public static void fsw_Deleted(Object source, FileSystemEventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
            sw.WriteLine("Supression de fichiers" +e.Name + " " + e.FullPath);
            sw.Close();
 
            string Path = e.FullPath;
            string[] chemin = Path.Split('\\');
            string fin = chemin[0] + '/' + e.Name;
 
            //string path = e.fullpath;
            //string[] chemin = path.split('\\');
            //string fin = chemin[0] + '/' + chemin[1];
 
            //string Nom = e.Name;
            //string CheminCopy = ("@D:/COPIE/copie3.txt");
 
            //File.Copy(fin, CheminCopy, true);
 
            DeleteFile("ftp://172.16.212.113/Images", "Julien", "Julien", fin); 
        }
 
        //public static void fsw_Changed(Object source, FileSystemEventArgs e)
        //{
        //    StreamWriter sw = new StreamWriter(@"C:/WatchDogFolder.log", true);
        //    sw.WriteLine("Modification de fichiers" + e.Name + " " + e.FullPath);
        //    sw.Close();
        //    string Path = e.FullPath;
        //    string[] chemin = Path.Split('\\');
        //    string fin = chemin[0] + '/' + e.Name;
 
        //    string Path = e.FullPath;
        //    string[] chemin = Path.Split('\\');
        //    string fin = chemin[0] + '/' + e.Name;
 
 
        //    string Nom = e.Name;
        //    string CheminCopy = ("@D:/COPIE/copie3.txt");
 
        //    File.Copy(fin, CheminCopy, true);
        //    UpLoadFile("ftp://172.16.212.113/Images", "Julien", "Julien", fin);
        //}
 
        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 + " " + e.FullPath);
            sw.Close();
 
            string Path = e.FullPath;
            string[] chemin = Path.Split('\\');
            string fin = chemin[0] + '/' + e.Name;
            string fin2 = chemin[0] + '/' + e.OldName;
 
            DeleteFile("ftp://172.16.212.113/Images", "Julien", "Julien", fin2);
            UpLoadFile("ftp://172.16.212.113/Images", "Julien", "Julien", fin);
 
 
 
 
        }
 
        public static void UpLoadFile(string ftpserver, string username, string password, string filename)
        {
            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                client.Credentials = new System.Net.NetworkCredential(username, password);
                client.UploadFile(ftpserver + '/' + new FileInfo(filename).Name, "STOR", filename);                
            }        
        }
 
        public static void DeleteFile(string ftpserver, string username, string password, string filename)
        {          
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpserver + '/' + filename);                
                request.Credentials = new System.Net.NetworkCredential(username, password);
                request.Method = WebRequestMethods.Ftp.DeleteFile; 
                FtpWebResponse response  = (FtpWebResponse)request.GetResponse();
                response.Close();              
        }
    }
 
 
}

Auriez-vous des idées de pourquoi la fonction de suppression ne fonctionne pas ?
Merci par avance

Julien