我正在尝试使用 System.Diagnostics.Process 从 c# 代码中安装 VHD 文件以执行 diskpart.exe 命令。
下面是我使用的代码片段:
process.StartInfo.FileName = "diskpart.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
StreamWriter writer = process.StandardInput;
writer.Write("select vdisk file=" + vhdfile + System.Environment.NewLine);
writer.Write("attach vdisk" + System.Environment.NewLine);
writer.Write("online disk" + System.Environment.NewLine);
writer.Write("select part 1" + System.Environment.NewLine);
writer.Write("remove all" + System.Environment.NewLine);
writer.Write("assign mount=" + mountpoint + System.Environment.NewLine);
writer.Close();
process.WaitForExit();
process.Close();
问题是如果我在命令提示符下执行相同的命令(使用批处理文件),执行大约需要 0.5 秒。但是如果我运行上面的代码,WaitForExit() 大约需要 5 秒才能返回。
我尝试将 RedirectStandardOutput 和 RedirectStandardError 设置为 true 和 false,但我看不出有什么区别。
请让我知道我在这里可能做错了什么。
谢谢。