WinForm 程序调用DOS命令

//创建进程对象
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
//设定需要执行的命令
startInfo.FileName = "cmd.exe"; 
//参数“/C”表示执行完命令后马上退出
startInfo.Arguments = "/C " + command;
//不使用系统外壳程序启动
startInfo.UseShellExecute = false; 
//不重定向输入
startInfo.RedirectStandardInput = false; 
//重定向输出
startInfo.RedirectStandardOutput = true;
//不创建窗口
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
try{
    //开始进程
    if (process.Start()) {
        //milliseconds等待时长,等于零表示等待进程结束
        if (milliseconds== 0) {
            //这里无限等待进程结束
            process.WaitForExit();
        } else {
            //等待进程结束,等待时间为指定的毫秒
            process.WaitForExit(milliseconds); 
        }
        //读取进程的输出
        output = process.StandardOutput.ReadToEnd();
    }
}catch{
}finally{
    if (process != null){
        process.Close();
    }
}

results matching ""

    No results matching ""