我目前正在尝试通过命令行断开网络文件夹,并使用以下代码:
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”到标准输入?
答案 0 :(得分:1)
使用 代码下面的消息“是否可以继续断开连接并强行关闭?(是/否)[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();
}
我认为这可能对你有帮助..