Ejemplo n.º 1
0
        public static ExecuteCommandResult ExecuteCommand(string command, DelayedEvent delayedEvent, Section section, Settings settings, string settingsPrefix)
        {
            var filename = delayedEvent.FileSystemEvent.FullPath;

            if (command.StartsWith(SHELL_COMMAND_PREFIX))
            {
                return(ExecuteShellCommandSync(command.Substring(1), filename));
            }
            try
            {
                var tmp          = command.Split(INTERNAL_COMMAND_SEPARATOR);
                var assemblyFile = tmp[0];
                var subNamespace = tmp[1];
                var className    = tmp[2];

                Type myType;
                if (assemblyFile == "none")
                {
                    myType = Type.GetType(string.Format("FileSysWatcher.Modules.{0}", className));
                }
                else
                {
                    // Use the file name to load the assembly into the current
                    // application domain.
                    var a = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + assemblyFile);
                    // Get the type to use.
                    myType = a.GetType(string.Format("FileSysWatcher.Modules.{0}.{1}", subNamespace, className));
                }
                // Get the method to call.
                var mymethod = myType.GetMethod("Execute");
                // Create an instance.
                Object obj = Activator.CreateInstance(myType, section, settings, settingsPrefix);
                Debug.Assert(obj.GetType() == typeof(ModuleBase));
                // Execute the method.
                var    parameters = new object[] { delayedEvent };
                object result     = mymethod.Invoke(obj, parameters);
                Debug.Assert(result.GetType() == typeof(ExecuteCommandResult));
                return((ExecuteCommandResult)result);
            }
            catch (Exception ex)
            {
                var result = new ExecuteCommandResult();
                result.Error = ex.InnerException ?? ex;
                return(result);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes a shell command synchronously.
        /// </summary>
        /// <param name="command">format command</param>
        /// <param name="filename"> </param>
        /// <returns>string, as output of the command.</returns>
        public static ExecuteCommandResult ExecuteShellCommandSync(string command, string filename)
        {
            var result = new ExecuteCommandResult {
                IsShellCommand = true
            };

            try
            {
                command = FormatCommand(command, filename);
                // create the ProcessStartInfo using "cmd" as the program to be run,
                // and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows,
                // and then exit.
                var procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.RedirectStandardError  = true;
                procStartInfo.UseShellExecute        = false;

                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;

                // Now we create a process, assign its ProcessStartInfo and start it
                var proc = new Process {
                    StartInfo = procStartInfo
                };
                proc.Start();

                // read the results
                string errors = proc.StandardError.ReadToEnd();
                result.Lines.AddRange(errors.Split(new[] { '\r', '\n' }));
                string output = proc.StandardOutput.ReadToEnd();
                result.Lines.AddRange(output.Split(new[] { '\r', '\n' }));
                result.ExitCode = proc.ExitCode;
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }
            return(result);
        }
Ejemplo n.º 3
0
        public override ExecuteCommandResult Execute(DelayedEvent delayedEvent)
        {
            var result = new ExecuteCommandResult();

            if (minimumStableTime.TotalMilliseconds == 0)
            {
                return(result);
            }

            var filename = delayedEvent.FileSystemEvent.FullPath;

            if (!File.Exists(filename))
            {
                result.ExitCode = deletedCountsAsStable ? 0 : 1;
                return(result);
            }
            if (File.GetLastWriteTime(filename) > DateTime.Now - minimumStableTime)
            {
                result.ExitCode = 2;
                return(result);
            }

            return(result);
        }