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?

7
  • possible duplicate of How to spawn a process and capture its STDOUT in .NET? Commented Oct 23, 2013 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. Commented Oct 23, 2013 at 20:55
  • 1
    @MichaC He's directing data to the process, not from it. Commented Oct 23, 2013 at 20:55
  • try Console.WriteLine Commented Oct 23, 2013 at 20:55
  • 1
    @Matt I think you need to read past the title of the question. Commented Oct 23, 2013 at 20:56

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.