protected override void Execute(CodeActivityContext context) { string _arguments = Arguments.Get(context); string _fileName = FileName.Get(context); string _workingDirectory = WorkingDirectory.Get(context); try { Process p = new System.Diagnostics.Process(); if (_arguments != null) { p.StartInfo.Arguments = _arguments; } if (_workingDirectory != null) { p.StartInfo.WorkingDirectory = _workingDirectory; } p.StartInfo.UseShellExecute = Default; p.StartInfo.Verb = "Open"; p.StartInfo.FileName = _fileName; p.Start(); } catch (Exception e) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "有一个错误产生", e.Message); if (!ContinueOnError.Get(context)) { throw new NotImplementedException(e.Message); } } }
protected override void Execute(NativeActivityContext context) { try { string _arguments = Arguments.Get(context); string _fileName = ProcessPath.Get(context); string _workingDirectory = WorkingDirectory.Get(context); Int32 _timeout = Timeout.Get(context); Process p = new System.Diagnostics.Process(); if (_arguments != null) { p.StartInfo.Arguments = _arguments; } if (_workingDirectory != null) { p.StartInfo.WorkingDirectory = _workingDirectory; } p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = _fileName; p.Start(); Thread.Sleep(_timeout); context.ScheduleAction(Body, "", OnCompleted, OnFaulted); } catch (Exception e) { if (ContinueOnError.Get(context)) { } else { throw new NotImplementedException(e.Message); } } }
protected override void Execute(CodeActivityContext context) { var filename = Filename.Get(context); var arguments = Arguments.Get(context); var workingdirectory = WorkingDirectory.Get(context); var waitforexit = WaitForExit.Get(context); var waitforexittimeout = WaitForExitTimeout.Get(context); ProcessStartInfo startInfo = new ProcessStartInfo(); filename = Environment.ExpandEnvironmentVariables(filename); startInfo.FileName = filename; startInfo.Arguments = arguments; startInfo.WorkingDirectory = workingdirectory; var p = Process.Start(startInfo); if (waitforexit) { if (waitforexittimeout.TotalMilliseconds > 50) { p.WaitForExit((int)waitforexittimeout.TotalMilliseconds); } else { p.WaitForExit(); } } }
protected void ValidateWorkingDirectory(CodeActivityContext context) { var workingDirectory = WorkingDirectory.Get(context); if (!string.IsNullOrEmpty(workingDirectory) && !Directory.Exists(workingDirectory)) { throw new ArgumentException("Working directory does not exist."); } }
protected override GeneratorDirectorySet Execute(CodeActivityContext context) { var dirSet = new GeneratorDirectorySet(); dirSet.WorkingDirectory = WorkingDirectory.Get(context); dirSet.NupkgStoreDirectory = NupkgStoreDirectory.Get(context); dirSet.CreateWorkingDirectoryIfNotExists = CreateWorkingDirectoryIfNotExists.Get(context); dirSet.CreateNupkgStoreDirectoryIfNotExists = CreateNupkgStoreDirectoryIfNotExists.Get(context); dirSet.CleanWorkingDirectory = CleanWorkingDirectory.Get(context); if (EnsureDirectoriesAreValid.Get(context)) { DirectoriesAreValid.Set(context, dirSet.EnsureDirectories()); } return(dirSet); }
protected override void Execute(CodeActivityContext context) { ValidateInputArguments(context); var processInvokeWrapper = new RunProcessWrapper(FileName.Get(context), Arguments.Get(context), WorkingDirectory.Get(context), CaptureOutput.Get(context), WaitForExit.Get(context), WaitForExitTimeout.Get(context), KillAtTimeout.Get(context)); processInvokeWrapper.StartProcess(); ProcessId.Set(context, processInvokeWrapper.ProcessId); Finished.Set(context, processInvokeWrapper.Finished); Output.Set(context, processInvokeWrapper.StandardOutput); Error.Set(context, processInvokeWrapper.StandardError); ExitCode.Set(context, processInvokeWrapper.ExitCode); }
protected virtual void ValidateFilename(CodeActivityContext context, bool lookForFilesInPATH) { var workingDirectory = WorkingDirectory.Get(context); if (string.IsNullOrEmpty(workingDirectory)) { workingDirectory = Directory.GetCurrentDirectory(); } var filename = FileName.Get(context); if (Path.IsPathRooted(filename) && !File.Exists(filename)) { throw new ArgumentException("Filename with absolute path could not be found."); } else { if (Path.GetFileName(filename) == filename) { if (lookForFilesInPATH && !Environment.GetEnvironmentVariable("PATH").Split(';'). Any(dir => (string.IsNullOrEmpty(Path.GetExtension(filename)) && // The given filename does not have an extension but matches one file from the directories in PATH Directory.GetFiles(dir).Any(file => Path.GetFileNameWithoutExtension(file) == filename)) || File.Exists(Path.Combine(dir, filename)))) // Filename is present in one of the directories from PATH { throw new ArgumentException("Filename could not be found in the working directory or in any directory from the PATH Environment Variable."); } } else { if (!File.Exists(Path.Combine(workingDirectory, filename))) { throw new ArgumentException("Filename with relative path could not be found in the current working directory."); } } } }
// Module defining this command // Optional custom code for this activity /// <summary> /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run. /// </summary> /// <param name="context">The NativeActivityContext for the currently running activity.</param> /// <returns>A populated instance of Sytem.Management.Automation.PowerShell</returns> /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks> protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context) { System.Management.Automation.PowerShell invoker = global::System.Management.Automation.PowerShell.Create(); System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName); // Initialize the arguments if (FilePath.Expression != null) { targetCommand.AddParameter("FilePath", FilePath.Get(context)); } if (ArgumentList.Expression != null) { targetCommand.AddParameter("ArgumentList", ArgumentList.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (WorkingDirectory.Expression != null) { targetCommand.AddParameter("WorkingDirectory", WorkingDirectory.Get(context)); } if (LoadUserProfile.Expression != null) { targetCommand.AddParameter("LoadUserProfile", LoadUserProfile.Get(context)); } if (NoNewWindow.Expression != null) { targetCommand.AddParameter("NoNewWindow", NoNewWindow.Get(context)); } if (PassThru.Expression != null) { targetCommand.AddParameter("PassThru", PassThru.Get(context)); } if (RedirectStandardError.Expression != null) { targetCommand.AddParameter("RedirectStandardError", RedirectStandardError.Get(context)); } if (RedirectStandardInput.Expression != null) { targetCommand.AddParameter("RedirectStandardInput", RedirectStandardInput.Get(context)); } if (RedirectStandardOutput.Expression != null) { targetCommand.AddParameter("RedirectStandardOutput", RedirectStandardOutput.Get(context)); } if (Verb.Expression != null) { targetCommand.AddParameter("Verb", Verb.Get(context)); } if (WindowStyle.Expression != null) { targetCommand.AddParameter("WindowStyle", WindowStyle.Get(context)); } if (Wait.Expression != null) { targetCommand.AddParameter("Wait", Wait.Get(context)); } if (UseNewEnvironment.Expression != null) { targetCommand.AddParameter("UseNewEnvironment", UseNewEnvironment.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
private void ExecuteInvoke(object contextObject) { CodeActivityContext context = contextObject as CodeActivityContext; string commandLine = FileName.Get(context) + " " + Arguments.Get(context); bool useShellExecute = UseShellExecute.Get <bool>(context); bool showWindow = ShowWindow.Get <bool>(context); string stdOut = string.Empty; string errOut = string.Empty; bool isNative = IsNative.Get(context); WriteLineConsole((useShellExecute ? "Open : " : "") + commandLine); Process processRunner = new Process(); if (isNative) { processRunner.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC"); } else { processRunner.StartInfo.FileName = FileName.Get(context); processRunner.StartInfo.Arguments = Arguments.Get(context); } processRunner.StartInfo.WorkingDirectory = WorkingDirectory.Get <string>(context); processRunner.StartInfo.UseShellExecute = useShellExecute; processRunner.StartInfo.CreateNoWindow = showWindow; processRunner.StartInfo.Verb = Verb.Get <string>(context); if (!useShellExecute && !showWindow) { processRunner.StartInfo.RedirectStandardOutput = true; processRunner.StartInfo.RedirectStandardError = true; processRunner.StartInfo.RedirectStandardInput = true; } try { processRunner.Start(); } catch (Exception ex) { ExitCode.Set(context, 1); if (ThrowOnExitCode.Get <bool>(context)) { throw new InvalidOperationException(ex.Message); } else { WriteLineConsole(ex.Message); Output.Set(context, ex.Message); } return; } if (!useShellExecute && !showWindow) { if (IsNative.Get(context)) { processRunner.StandardInput.WriteLine("echo off"); processRunner.StandardInput.WriteLine(commandLine); processRunner.StandardInput.WriteLine("exit"); } string outPutLine; string outPut = string.Empty; bool blockedOutput = IsNative.Get(context); while ((outPutLine = processRunner.StandardOutput.ReadLine()) != null) { if ((blockedOutput) || (outPutLine == "exit")) { if (outPutLine == commandLine) { blockedOutput = false; } continue; } stdOut += outPutLine + "\n"; WriteLineConsole(outPutLine); } while ((outPutLine = processRunner.StandardError.ReadLine()) != null) { if ((blockedOutput) || (outPutLine == "exit")) { if (outPutLine == commandLine) { blockedOutput = false; } continue; } errOut += outPutLine + "\n"; WriteLineConsole(outPutLine); } processRunner.WaitForExit(); ExitCode.Set(context, processRunner.ExitCode); bool success = (processRunner.ExitCode == 0); if (success) { Output.Set(context, stdOut); } else { if (!ThrowOnExitCode.Get <bool>(context)) { Output.Set(context, errOut); } } if (processRunner != null && !processRunner.HasExited) { processRunner.Kill(); processRunner = null; } if (!success && ThrowOnExitCode.Get <bool>(context)) { throw new InvalidOperationException(errOut); } } }
/// <summary> /// See http://taskscheduler.codeplex.com/ for help on using the TaskService wrapper library. /// </summary> /// <param name="context"></param> protected override void Execute(CodeActivityContext context) { try { using (TaskService ts = new TaskService()) { TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = TaskDescription.Get(context); var triggers = Triggers.Get(context); if (triggers != null) { triggers.ToList().ForEach(t => td.Triggers.Add(t)); } td.Actions.Add(new ExecAction(FileName.Get(context), Arguments.Get(context), WorkingDirectory.Get(context))); string username = Username.Get(context); string password = Password.Get(context); if (string.IsNullOrEmpty(username)) { username = WindowsIdentity.GetCurrent().Name; } ts.RootFolder.RegisterTaskDefinition( TaskName.Get(context), td, TaskCreation.CreateOrUpdate, username, password); } } catch { throw; } }