Bonjour à tous,
Je cherche à créer un service windows qui permet de déplacer un type de fichier (.txt, .xls...) d'un dossier source vers un dossier de destination. Mon service doit pouvoir fonctionner selon 3 modes:
- temps réel
- a heure fixe
- tous les X minutes
Pour l'instant je me concentre sur le fonctionnement en temps réel mais je bloque un peu. Voici mon code:
En l'état mon service démarre sans souci, par contre si je rajoute des fichiers dans mon dossier source, rien ne se passe. De plus l'arrêt du service plante avec l'erreur 1016.
Code : 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 using System.Text; using System.Configuration.Install; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.IO; using System.Threading; using System.Configuration; namespace WindowsServiceDVP { [RunInstaller(true)] public class ServiceInstall : Installer { public ServiceInstall() : base() { ServiceProcessInstaller process = new ServiceProcessInstaller(); process.Account = ServiceAccount.LocalSystem; ServiceInstaller service = new ServiceInstaller(); service.StartType = ServiceStartMode.Automatic; service.ServiceName = "MoveFiles"; service.DisplayName = "MoveFiles"; service.Description = "Service de déplacement de fichier"; Installers.Add(service); Installers.Add(process); } } public partial class Service1 : ServiceBase { public string mode; private FileSystemWatcher watcher; private Timer Schedular; public Service1() { ServiceInstaller si = new ServiceInstaller(); si.StartType = ServiceStartMode.Automatic; si.DisplayName = "MoveFiles"; si.Description = "Service de déplacement de fichier"; si.ServiceName = "MoveFiles"; InitializeComponent(); } protected override void OnStart(string[] args) { watcher = new FileSystemWatcher(); watcher.Path = ConfigurationManager.AppSettings["SourceFolder"]; watcher.Filter = ConfigurationManager.AppSettings["FileType"]; watcher.EnableRaisingEvents = true; Library.WriteErrorLog("le Service MoveFiles est démarré"); // recuperation du mode de synchronisation mode = ConfigurationManager.AppSettings["Mode"].ToUpper(); Library.WriteErrorLog("Mode de mise à jour:" + mode); switch (mode) { case "DAILY": Library.WriteErrorLog("Copie des fichiers à : " + System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]); // A completer break; case "INTERVAL": Library.WriteErrorLog("Copie des fichiers toutes les : " + ConfigurationManager.AppSettings["IntervalMinutes"] + " minutes."); // A completer break; case "REALTIME": watcher.Changed += new FileSystemEventHandler(OnChanged); Library.WriteErrorLog("Copie des fichiers en temps réel"); break; } } protected override void OnStop() { watcher.EnableRaisingEvents = false; Schedular.Dispose(); Library.WriteErrorLog("le Service MoveFiles est stoppé"); } private static void OnChanged(object source, FileSystemEventArgs e) { try { string[] Files = Directory.GetFiles(e.FullPath, ConfigurationManager.AppSettings["FileType"]); foreach (var item in Files) { File.Move(item, Path.Combine(ConfigurationManager.AppSettings["DestFolder"], Path.GetFileName(item))); } Library.WriteErrorLog("fichiers copiés"); } catch { } } } }
Pouvez-vous me mettre sur la voie?
Merci pour votre aide
Partager