简体   繁体   English

C#进程在Process.Start()之后被取消

[英]C# process being cancelled after Process.Start()

I'm trying to run Command Prompt commands from C# code. 我正在尝试从C#代码运行命令提示符命令。

After Process.Start() step is executed, console window shows 执行Process.Start()步骤后,控制台窗口显示

System error 1223 has occured. 发生系统错误1223。 The operation was cancelled by the user. 该操作已被用户取消。

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. 但是我仍然遇到同样的错误。

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 这将起作用...我使用/ K开关,因为我不想终止CMDscreen并想查看结果。.如果需要自动终止,请使用/ C

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

==========Result ============================ ==========结果===========================

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM