private void ProcessFiles(object cmd) { ChoAppSettings appSettings = cmd as ChoAppSettings; // cmd.ToString(); if (appSettings == null) { return; } try { IsRunning = true; _roboCopyManager = new ChoRoboCopyManager(); _roboCopyManager.Status += (sender, e) => SetStatusMsg(e.Message); _roboCopyManager.AppStatus += (sender, e) => UpdateStatus(e.Message, e.Tag.ToNString()); _roboCopyManager.Process(appSettings.RoboCopyFilePath, appSettings.GetCmdLineParams(), appSettings); } catch (ThreadAbortException) { } catch (Exception ex) { SetStatusMsg(ex.ToString()); } finally { IsRunning = false; _roboCopyManager = null; } }
private void MyWindow_Loaded(object sender1, RoutedEventArgs e1) { _bindObj = new ChoWPFBindableConfigObject <ChoAppSettings>(); _appSettings = _bindObj.UnderlyingSource; _appSettings.Init(); if (!SettingsFilePath.IsNullOrWhiteSpace() && File.Exists(SettingsFilePath)) { _appSettings.LoadXml(File.ReadAllText(SettingsFilePath)); } else { _appSettings.Reset(); } DataContext = this; _mainUIThread = Thread.CurrentThread; btnNewFile_Click(null, null); _dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); _dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); _dispatcherTimer.Start(); string x = _appSettings.SourceDirectory; ChoShellExtCmdLineArgs cmdLineArgs = new ChoShellExtCmdLineArgs(); if (!cmdLineArgs.Directory.IsNullOrWhiteSpace()) { _appSettings.SourceDirectory = cmdLineArgs.Directory; } IsDirty = false; }
private string MarshalCmd(string cmd, ChoAppSettings appSettings) { if (cmd != null) { cmd = cmd.Replace(@"{SRC_DIR}", appSettings.SourceDirectory); cmd = cmd.Replace(@"{DEST_DIR}", appSettings.DestDirectory); } return(cmd); }
public void StartFileCopy(string sourceDirectory = null, string destDirectory = null) { try { ChoAppSettings appSettings = new ChoAppSettings(); if (!SettingsFilePath.IsNullOrWhiteSpace()) { if (!File.Exists(SettingsFilePath)) { throw new ArgumentException("Can't find '{0}' settings file.".FormatString(SettingsFilePath)); } appSettings.LoadXml(File.ReadAllText(SettingsFilePath)); } ChoConsole.WriteLine(); ChoRoboCopyManager _roboCopyManager = new ChoRoboCopyManager(SettingsFilePath); _roboCopyManager.Status += (sender, e) => { ChoTrace.Write(e.Message); ChoConsole.Write(e.Message, ConsoleColor.Yellow); }; _roboCopyManager.AppStatus += (sender, e) => { ChoTrace.Write(e.Message); ChoConsole.Write(e.Message, ConsoleColor.Yellow); }; _roboCopyManager.Process(appSettings.RoboCopyFilePath, appSettings.GetCmdLineParams(), appSettings, true); } catch (ThreadAbortException) { Console.WriteLine("RoboCopy operation cancelled by user."); } catch (Exception ex) { Console.WriteLine("RoboCopy operation failed." + Environment.NewLine + ChoApplicationException.ToString(ex)); } }
public void Process(string fileName, string arguments, ChoAppSettings appSettings, bool console = false) { AppStatus.Raise(this, new ChoFileProcessEventArgs("Starting RoboCopy operation...")); Status.Raise(this, new ChoFileProcessEventArgs(Environment.NewLine)); string preCommands = appSettings.Precommands; string postCommands = appSettings.Postcommands; try { // Setup the process start info var processStartInfo = new ProcessStartInfo("cmd.exe", " /K /E:OFF /F:OFF /V:OFF") // new ProcessStartInfo(fileName, arguments) //_appSettings.RoboCopyFilePath, _appSettings.GetCmdLineParams(sourceDirectory, destDirectory)) { UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; // Setup the process Process process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true }; // Register event _process = process; // Start process process.Start(); //process.BeginOutputReadLine(); Task.Factory.StartNew(new Action <object>(ReadFromStreamReader), process.StandardOutput); //Task.Factory.StartNew(new Action<object>(ReadFromStreamReader), process.StandardError); if (!console) { process.StandardInput.WriteLine("prompt $G"); } //Run precommands if (!preCommands.IsNullOrWhiteSpace()) { foreach (var cmd in preCommands.SplitNTrim().Select(c => c.NTrim()).Select(c => MarshalCmd(c, appSettings)).Where(c => !c.IsNullOrWhiteSpace())) { process.StandardInput.WriteLine($"{cmd}"); } } process.StandardInput.WriteLine($"{fileName} {arguments}"); //Run postcommands if (!postCommands.IsNullOrWhiteSpace()) { foreach (var cmd in postCommands.SplitNTrim().Select(c => c.NTrim()).Select(c => MarshalCmd(c, appSettings)).Where(c => !c.IsNullOrWhiteSpace())) { process.StandardInput.WriteLine($"{cmd}"); } } process.StandardInput.WriteLine("exit"); process.WaitForExit(); _process = null; AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation completed successfully.", "RoboCopy operation completed successfully")); } catch (ThreadAbortException) { Status.Raise(this, new ChoFileProcessEventArgs(Environment.NewLine + "RoboCopy operation canceled by user." + Environment.NewLine, "RoboCopy operation failed.")); AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation canceled by user.", "RoboCopy operation failed.")); } catch (Exception ex) { Status.Raise(this, new ChoFileProcessEventArgs(Environment.NewLine + ex.ToString() + Environment.NewLine)); AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation failed.", "RoboCopy operation failed.")); } }
private void MyWindow_Loaded(object sender1, RoutedEventArgs e1) { _bindObj = new ChoWPFBindableConfigObject <ChoAppSettings>(); _appSettings = _bindObj.UnderlyingSource; _appSettings.Init(); if (!SettingsFilePath.IsNullOrWhiteSpace() && File.Exists(SettingsFilePath)) { _appSettings.LoadXml(File.ReadAllText(SettingsFilePath)); } else { _appSettings.Reset(); } this.DataContext = _appSettings; _appSettings.BeforeConfigurationObjectLoaded += ((o, e) => { e.Cancel = (bool)this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Func <bool>(() => { if (IsDirty) { if (MessageBox.Show("Configuration settings has been modified outside of the tool. {0}Do you want to reload it and lose the changes made in the tool?".FormatString(Environment.NewLine), Title, MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { return(false); } else { return(true); } } return(false); })); }); _appSettings.AfterConfigurationObjectMemberSet += ((o, e) => { if (_wndLoaded) { IsDirty = true; } this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => { txtRoboCopyCmd.Text = _appSettings.GetCmdLineText(); })); }); _appSettings.ConfigurationObjectMemberLoadError += _appSettings_ConfigurationObjectMemberLoadError; _appSettings.AfterConfigurationObjectPersisted += _appSettings_AfterConfigurationObjectPersisted; _appSettings.AfterConfigurationObjectLoaded += ((o, e) => { if (_wndLoaded) { this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => { this.DataContext = null; this.DataContext = _appSettings; txtRoboCopyCmd.Text = _appSettings.GetCmdLineText(); IsDirty = false; })); } else { this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => { txtRoboCopyCmd.Text = _appSettings.GetCmdLineText(); })); } }); _mainUIThread = Thread.CurrentThread; _dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); _dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); _dispatcherTimer.Start(); string x = _appSettings.SourceDirectory; ChoShellExtCmdLineArgs cmdLineArgs = new ChoShellExtCmdLineArgs(); if (!cmdLineArgs.Directory.IsNullOrWhiteSpace()) { _appSettings.SourceDirectory = cmdLineArgs.Directory; } IsDirty = false; }