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.AttachAction(poll, FolderPollAction.Copy, watcher); } if (poll.NewFile.Move != null) { this.AttachAction(poll, FolderPollAction.Move, watcher); } if (poll.NewFile.Launch != null) { this.AttachAction(poll, FolderPollAction.Launch, watcher); } // 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); } }
/// <summary> /// Generation of Polls, test directiories creation /// </summary> /// <param name="impersonation">Is use impersonation or not</param> private void PrepareTestEnviroment(bool impersonation) { // fill random input folders var rndInputFoldersCount = rnd.Next(10, 21); // from 10 to 20 Polls CreateRootFolders(); for (int i = 0; i < rndInputFoldersCount; i++) { string folderName = Guid.NewGuid().ToString(); string targetFolder; var poll = new Poll { Impersonation = impersonation }; if (impersonation) { poll.Domain = ""; poll.Username = this.ProtectedUser.UserName; poll.Password = this.ProtectedUser.UserPassword; targetFolder = ProtectedTestFolderPath; } else { targetFolder = TestFolderPath; } poll.Folder = Path.Combine(targetFolder, folderName); poll.NewFile = new NewFile { Filter = "*." + TestHelper.GetRandomAlphaNumericFileExtension(3) }; // Random Action: 0 - CopyFile, 1 - MoveFile, 2 - Copy And Move var variant = rnd.Next(0, 3); if (variant == 0 || variant == 2) { poll.NewFile.Copy = new NewFileCopy { TargetFolder = Path.Combine(targetFolder, "out_copy_" + folderName), }; } if (variant == 1 || variant == 2) { poll.NewFile.Move = new NewFileMove { TargetFolder = Path.Combine(targetFolder, "out_move_" + folderName) }; } polls.Add(poll); CreatePollFolders(poll, impersonation); } }
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 CreatePollFolders(Poll poll, bool isProtected) { if (!isProtected) { Directory.CreateDirectory(poll.Folder); if (poll.NewFile.Copy != null) { Directory.CreateDirectory(poll.NewFile.Copy.TargetFolder); } if (poll.NewFile.Move != null) { Directory.CreateDirectory(poll.NewFile.Move.TargetFolder); } } else { var context = new PrincipalContext(ContextType.Machine); FolderProtectHelper.SetupSecurityGroupAndUser(context, ProtectedUser.GroupName, ProtectedUser.GroupDescription, ProtectedUser.UserName, ProtectedUser.UserPassword, ProtectedUser.UserDescription); string currentUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; FolderProtectHelper.ProtectFolder(poll.Folder, ProtectedUser.UserName, currentUserName, true); if (poll.NewFile.Copy != null) { FolderProtectHelper.ProtectFolder(poll.NewFile.Copy.TargetFolder, ProtectedUser.UserName, currentUserName, true); } if (poll.NewFile.Move != null) { FolderProtectHelper.ProtectFolder(poll.NewFile.Move.TargetFolder, ProtectedUser.UserName, currentUserName, true); } } }
private static void AttachAction(Poll poll, FolderPollAction action, FileSystemWatcher watcher) { if (poll.Impersonation && poll.ImpersonationSpecified) { switch (action) { case FolderPollAction.Copy: watcher.Created += delegate(object sender, FileSystemEventArgs args) { using (new ImpersonatedUser(poll.Domain, poll.Username, poll.Password)) { CopyFile(args.FullPath, Path.Combine(poll.NewFile.Copy.TargetFolder, args.Name)); } }; break; case FolderPollAction.Move: watcher.Created += delegate(object sender, FileSystemEventArgs args) { using (new ImpersonatedUser(poll.Domain, poll.Username, poll.Password)) { MoveFile(args.FullPath, Path.Combine(poll.NewFile.Move.TargetFolder, args.Name)); } }; break; case FolderPollAction.Launch: watcher.Created += delegate(object sender, FileSystemEventArgs args) { using (new ImpersonatedUser(poll.Domain, poll.Username, poll.Password)) { LaunchApplication(poll.NewFile.Launch.Application, PrepareLaunchArgs(poll, args.FullPath, args.Name)); } }; break; } } else { switch (action) { case FolderPollAction.Copy: watcher.Created += (sender, args) => CopyFile(args.FullPath, Path.Combine(poll.NewFile.Copy.TargetFolder, args.Name)); break; case FolderPollAction.Move: watcher.Created += (sender, args) => MoveFile(args.FullPath, Path.Combine(poll.NewFile.Move.TargetFolder, args.Name)); break; case FolderPollAction.Launch: watcher.Created += (sender, args) => LaunchApplication(poll.NewFile.Launch.Application, PrepareLaunchArgs(poll, args.FullPath, args.Name)); break; } } }
private static string PrepareLaunchArgs(Poll poll, string fullPath, string nameWithExt) { if (string.IsNullOrEmpty(poll.NewFile.Launch.Arguments)) { return string.Empty; } string copiedFileRef = string.Empty; string movedFileRef = string.Empty; if (poll.NewFile.Copy != null && !string.IsNullOrEmpty(poll.NewFile.Copy.TargetFolder)) { copiedFileRef = Path.Combine(poll.NewFile.Copy.TargetFolder, nameWithExt); } if (poll.NewFile.Move != null && !string.IsNullOrEmpty(poll.NewFile.Move.TargetFolder)) { movedFileRef = Path.Combine(poll.NewFile.Move.TargetFolder, nameWithExt); } string nameWithoutExt = Path.GetFileName(nameWithExt); return string.Format(poll.NewFile.Launch.Arguments, fullPath, copiedFileRef, movedFileRef, nameWithExt, nameWithoutExt); }
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 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 { } }