Esempio n. 1
0
        internal void Cancel()
        {
            Process process = _process;

            if (process == null)
            {
                return;
            }

            try
            {
                process.Kill();
                AppStatus.Raise(this, new ChoFileProcessEventArgs("RoboCopy operation canceled."));
                _process = null;
            }
            catch { }
        }
Esempio n. 2
0
        public void Process(string fileName, string arguments)
        {
            AppStatus.Raise(this, new ChoFileProcessEventArgs("Starting RoboCopy operation..."));
            Status.Raise(this, new ChoFileProcessEventArgs(Environment.NewLine));

            try
            {
                // Setup the process start info
                var processStartInfo = new ProcessStartInfo(fileName, arguments) //_appSettings.RoboCopyFilePath, _appSettings.GetCmdLineParams(sourceDirectory, destDirectory))
                {
                    UseShellExecute        = false,
                    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);
                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."));
            }
        }
Esempio n. 3
0
        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."));
            }
        }