public void Run() { watchers.Clear(); foreach (var poll in this.folderPoll.Poll) { this.ProcessPoll(poll); try { if (poll.ImpersonationSpecified && poll.Impersonation) { using (var user = new ImpersonatedUser(poll.Username, poll.Password)) { this.ProcessPoll(poll); } } else { this.ProcessPoll(poll); } } catch (Exception) { } } }
private void ProcessPoll(Poll poll) { if (poll == null || poll.NewFile == null) { return; } var watcher = new FileSystemWatcher(poll.Folder, poll.NewFile.Filter); if (poll.NewFile.Copy != null) { this.ImpersonatedAction(poll, FolderPollAction.Copy, watcher); if (poll.ImpersonationSpecified && poll.Impersonation) { watcher.Created += delegate(object sender, FileSystemEventArgs args) { using (var user = new ImpersonatedUser(poll.Username, poll.Password)) { this.CopyFile(args.FullPath, poll.NewFile.Copy.TargetFolder); } }; } else { watcher.Created += (sender, args) => this.CopyFile(args.FullPath, string.Format("{0}\\{1}", poll.NewFile.Copy.TargetFolder, args.Name)); } } if (poll.NewFile.Move != null) { watcher.Created += (sender, args) => this.MoveFile(args.FullPath, string.Format("{0}\\{1}", poll.NewFile.Move.TargetFolder, args.Name)); } if (poll.NewFile.Launch != null) { watcher.Created += (sender, args) => this.LaunchApplication(poll.NewFile.Launch.Application, args.FullPath, poll.NewFile.Launch.Arguments); } // Add wather to list and EnableRaisingEvents only if we have at least one action if (poll.NewFile.Copy != null || poll.NewFile.Move != null || poll.NewFile.Launch != null) { watcher.EnableRaisingEvents = true; this.watchers.Add(watcher); } }
private void AttachAction(Poll poll, FolderPollAction action, FileSystemWatcher watcher) { if (poll.ImpersonationSpecified && poll.Impersonation) { using (var user = new ImpersonatedUser(poll.Username, poll.Password)) { switch (action) { case FolderPollAction.Copy: watcher.Created += (sender, args) => this.CopyFile(args.FullPath, string.Format("{0}\\{1}", poll.NewFile.Move.TargetFolder, args.Name)); break; case FolderPollAction.Move: watcher.Created += (sender, args) => this.MoveFile(args.FullPath, string.Format("{0}\\{1}", poll.NewFile.Move.TargetFolder, args.Name)); break; case FolderPollAction.Launch: watcher.Created +=(sender, args) => this.LaunchApplication(poll.NewFile.Launch.Application, args.FullPath, poll.NewFile.Launch.Arguments); break; } } } else { } }
private void ImpersonatedAction(Poll poll, FolderPollAction action, FileSystemWatcher watcher) { if (poll.ImpersonationSpecified && poll.Impersonation) { using (var user = new ImpersonatedUser(poll.Username, poll.Password)) { switch (action) { case FolderPollAction.Copy: break; case FolderPollAction.Move: break; case : break; } } } else { } }