C# Process Standard Input

后端 未结 1 2036
闹比i
闹比i 2021-01-25 00:43

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 Sys         


        
1条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 01:18

    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..

    0 讨论(0)
提交回复
热议问题