C#处理标准输入

我目前正在尝试通过命令行断开网络文件夹,并使用以下代码:

System.Diagnostics.Process process2 = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/C NET USE F: /delete"; startInfo.RedirectStandardError = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; process2.StartInfo = startInfo; process2.Start(); StreamWriter sw = process2.StandardInput; sw.WriteLine("Y"); sw.Close(); process2.WaitForExit(); process2.Close(); 

偶尔,我收到消息“继续断开并强行关闭它们是否可以?(是/否)[N]”,我想回复“Y”,但我似乎遇到了问题。

有谁知道为什么我的代码没有输入标准输入“Y”?

使用下面的 代码来获取消息“是否可以继续断开连接并强制它们关闭?(是/否)[N]”,回复“Y”

 static void Main(string[] args) { System.Diagnostics.Process process2 = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/C NET USE F: /delete"; startInfo.RedirectStandardError = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; process2.StartInfo = startInfo; process2.Start(); Read(process2.StandardOutput); Read(process2.StandardError); while (true) process2.StandardInput.WriteLine("Y"); } private static void Read(StreamReader reader) { new Thread(() => { while (true) { int current; while ((current = reader.Read()) >= 0) Console.Write((char)current); } }).Start(); } 

我想这可能会对你有所帮助..