簡體   English   中英

C#流程標准輸入

[英]C# Process Standard Input

我目前正在嘗試通過命令行從網絡文件夾斷開連接,並且正在使用以下代碼:

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

我認為這可能對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM