1

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.

Does anyone know why my code is not inputting "Y" to standard input?

mastur cheef
  • 185
  • 1
  • 3
  • 15
  • possible duplicate of [How to spawn a process and capture its STDOUT in .NET?](http://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net) – MichaC Oct 23 '13 at 20:54
  • You're redirecting standard error and output without actually reading from either. If you don't want to see the result, don't redirect it. Also note the common deadlocking pitfalls when reading both standard output and error; the buffers can fill up and just sit there waiting forever if you're not ensuring they're emptied. – Servy Oct 23 '13 at 20:55
  • 1
    @MichaC He's directing data *to* the process, not from it. – Servy Oct 23 '13 at 20:55
  • try Console.WriteLine – Matt Oct 23 '13 at 20:55
  • 1
    @Matt I think you need to read past the title of the question. – Servy Oct 23 '13 at 20:56
  • @Servy, sorry, I misunderstood the question – Matt Oct 23 '13 at 20:58
  • Thanks @Servy, but removing those redirections doesn't seem to have an effect on the problem that I'm having. It seems as though "N" is automatically accepted when the message pops up – mastur cheef Oct 23 '13 at 21:02

1 Answers1

1

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

  • I tried I/O redirection as shown here for a *console* app that wanted to call another Windows executable without opening a new window - it only caused problems. What worked for me was simply setting UseShellExecute=false - nothing else, see [here](https://stackoverflow.com/a/47781912/63209). – Paul Dec 12 '17 at 21:54
  • It works but cpu usage remains quite high because of the three infinite loops. I bet there is a better way to achieve the same result – T-moty Jan 08 '23 at 18:01