1

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

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();

有时,我收到消息“可以继续断开连接并强制它们关闭吗?(Y/N)[N]”,我想回复“Y”,但我似乎遇到了问题。

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

4

1 回答 1

1

使用以下 代码获取消息“可以继续断开连接并强制它们关闭吗?(Y/N)[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();
}

我想这可能会帮助你..

于 2014-12-06T12:02:29.517 回答