C#实现cmd执行bat脚本的功能

private void buttun-Click(object sender, EventArgs e)
{
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    string dir = System.Environment.CurrentDirectory;
    proc.StartInfo.FileName = @"C:\Windows\system32\cmd.exe";
    proc.StartInfo.WorkingDirectory = dir;
    proc.StartInfo.UseShellExecute = true;
    proc.StartInfo.RedirectStandardInput = false;
    proc.StartInfo.RedirectStandardOutput = false;
    proc.StartInfo.RedirectStandardError = false;
    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    proc.StartInfo.CreateNoWindow = false;
    proc.StartInfo.Arguments = "/k script.bat";
    proc.Start();

    proc.WaitForExit();
    proc.Close();
}

注意:bat脚本script.bat文件存放的位置:如果使用的vs中的启动按钮,这个脚本应该放置于目录:bin/Debug

你可能感兴趣的:(C#,Windows上位机)