C#执行dos命令

c#中的Process类可方便的调用外部程序,所以我们可以通过调用cmd.exe程序 
加入参数 "/c " + 要执行的命令来执行一个dos命令 
(/c代表执行参数指定的命令后关闭cmd.exe /k参数则不关闭cmd.exe) 

 

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/k shutdown -a";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = false;
            p.Start();
            Response.Write(p.StandardOutput.ReadToEnd());

posted @ 2012-07-03 15:06  贺俊峰  阅读(293)  评论(0编辑  收藏  举报