0

I'm trying to run Command Prompt commands from C# code.

After Process.Start() step is executed, console window shows

System error 1223 has occured. The operation was cancelled by the user.

Error:

System Error 1223

But, as you can see, I'm not cancelling the operation.

My Code:

            Process process = new Process();

            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = $"/C NET USE {driveChar}: {URL}";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;

            process.Start(); // After this step, console window shows the above error

            StreamWriter streamWriter = process.StandardInput;

            streamWriter.WriteLine(username);

            ...
            ...

            // remaining code

What is going wrong? Any ideas on resolving this error?

EDIT:

I don't actually need to redirect standard output, so I modified my code to only redirect standard input. But I'm still getting the same error.

Nikhil
  • 6,493
  • 10
  • 31
  • 68
  • 1
    If you want to redirect both standard input and output, you have to [read at least one asynchronously](https://stackoverflow.com/questions/12566166/) or you will deadlock. – Dour High Arch May 13 '19 at 20:18
  • @DourHighArch I don't actually need to redirect standard output, so I modified my code to only redirect standard input. But I'm still getting the same error. – Nikhil May 13 '19 at 20:32

1 Answers1

1

This will work...I use /K switch as i don't want to terminate the CMDscreen and want to see the result..use /C if you need auto terminate

     string strCmdText;
     strCmdText = @"/K NET USE z: \\server\SharedFolderName";
     System.Diagnostics.Process.Start("CMD.exe", strCmdText);

==========Result ============================

enter image description here

HO LI Pin
  • 1,441
  • 13
  • 13
  • Thank you. I think this is the reason for that error. I couldn't get it to work with my original remote location, but was able to connect to other location. Must be an issue with that server. Anyway, I ended up choosing a different approach (https://serverfault.com/a/967134/413154) and needn't have to write to the process input. – Nikhil May 14 '19 at 15:13