C# 用cmd来执行指令 & call bat

way1 call xxx.bat

        string exefile = @"C:\123.bat"; //exefile="D:\\XX\\xxx.bat"        System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(exefile);        psi.UseShellExecute = false;//要让USER看到cmd画面就设true        //psi.CreateNoWindow = true;        //psi.RedirectStandardOutput = true;        //psi.RedirectStandardInput = true;        //psi.RedirectStandardError = true;        System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);        proc.WaitForExit();        if (proc != null)        {            proc.Close();            proc.Dispose();            proc = null;        }            

way2 call cmd.exe (和cmd.exe互动)

public string Exec(List<string> commandText){    Process p = new Process();    p.StartInfo.FileName = "cmd.exe";    p.StartInfo.UseShellExecute = false;    p.StartInfo.RedirectStandardInput = true;    p.StartInfo.RedirectStandardOutput = true;    p.StartInfo.RedirectStandardError = true;    p.StartInfo.CreateNoWindow = true;    string strOutput = null;    try    {        p.Start();        for (int i = 0; i < commandText.Count; i++)        {            string cmd = commandText[i];            p.StandardInput.WriteLine(cmd);        }        p.StandardInput.WriteLine("exit");        strOutput = p.StandardOutput.ReadToEnd();        p.WaitForExit();        p.Close();    }    catch (Exception e)    {        strOutput = e.Message;    }    return strOutput;}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章