C# 处理标准输入 [英] C# Process Standard Input
本文介绍了C# 处理标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试通过命令行与网络文件夹断开连接,并使用以下代码:
I am currently trying to disconnect from a network folder through the command line and am using the following code:
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();
有时,我收到消息可以继续断开连接并强制关闭它们吗?(是/否)[否]",对此我想回复是",但我似乎对此有疑问在职的.
Occasionally, I get the message "Is it ok to continue disconnecting and force them closed? (Y/N) [N]", to which I want to reply "Y", but I seem to be having issues with that working.
有谁知道为什么我的代码没有在标准输入中输入Y"?
Does anyone know why my code is not inputting "Y" to standard input?
推荐答案
使用下面 代码 得到消息可以继续断开连接并强制关闭它们吗?(Y/N) [N]",回复Y"
Use below code to get the message "Is it ok to continue disconnecting and force them closed? (Y/N) [N]", to which reply "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();
}
我认为这可能对您有所帮助..
I think this may help you..
这篇关于C# 处理标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文