1

I want to run multiple shell commands from c#. I've tried Process.Start(""); but it opens new instances for each command. I've also tried ProcessStartInfo with StreamWriter (ref.) but it doesn't work for me. I want to exeute following shell commands from my c# application.

timeout 10
taskkill /F /IM "v*"
taskkill /F /IM "B*"
timeout 5
shutdown /f /p

How can I run these commands one by one from my c# application?

Thanks in advance...

4

2 回答 2

3

Try Process.StartInfo.CreateNoWindow and Process.WaitForExit()

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

http://msdn.microsoft.com/en-us/library/fb4aw7b8.aspx

You can also eliminate the "timeout" commands by doing System.Threading.Thread.Sleep(5000)

Here is some code to get you started:

static void Main(string[] args)
{
    System.Threading.Thread.Sleep(10000);
    ExecuteCommandAndWait("taskkill", "/F /IM \"v*\"");
    ExecuteCommandAndWait("taskkill","/F /IM \"B*\"");
    System.Threading.Thread.Sleep(5000);
    ExecuteCommandAndWait("shutdown", "/f /p");
}

private static void ExecuteCommandAndWait(string fileName, string arguments)
{
    Process process = new Process();
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.FileName = fileName;
    process.StartInfo.Arguments = arguments;
    process.Start();
    process.WaitForExit();
}
于 2013-03-27T16:08:58.843 回答
2

Creating CMD file and running it is probably the easiest option.

Side note: shutdown /f /t 30 maybe an easier command.

于 2013-03-27T16:03:02.653 回答