简体   繁体   English

从C#运行CMD.exe时,系统找不到指定的文件

[英]The system cannot find the file specified when running CMD.exe from C#

I'm getting the error message when running the following code from a C# console program. 从C#控制台程序运行以下代码时收到错误消息。

"The system cannot find the file specified" “该系统找不到指定的文件”

Here is the code: 这是代码:

System.Diagnostics.Process.Start("C:\\Windows\\System32\\cmd.exe /c"); System.Diagnostics.Process.Start(“C:\\ Windows \\ System32 \\ cmd.exe / c”);

Strangely when i omit the /c switch the command can run!?! 奇怪的是,当我省略/ c开关时,命令可以运行!?!

Any ideas what I'm doing wrong? 我有什么想法我做错了吗?

Process.Start takes a filename as an argument. Process.Start将文件名作为参数。 Pass the argument as the second parameter: 将参数作为第二个参数传递:

System.Diagnostics.Process.Start(@"C:\Windows\System32\cmd.exe", "/c");

Well, for one thing, you're hard-coding a path, which is already destined to break on somebody's system (not every Windows install is in C:\\Windows ). 好吧,首先,你是一个硬编码的路径,它已经注定要破坏某人的系统(并非每个Windows安装都在C:\\Windows )。

But the problem here is that those backslashes are being used as an escape character. 但问题是这些反斜杠被用作转义字符。 There are two ways to write a path string like this - either escape the backslashes: 有两种方法可以像这样写一个路径字符串 - 要么转义反斜杠:

Process.Start("C:\\Windows\\System32\\cmd.exe", "/c");

Or use the @ to disable backslash escaping: 或者使用@来禁用反斜杠转义:

Process.Start(@"C:\Windows\System32\cmd.exe", "/c");

You also need to pass /c as an argument, not as part of the path - use the second overload of Process.Start as shown above. 您还需要将/c作为参数传递,而不是作为路径的一部分 - 使用Process.Start的第二个重载,如上所示。

There is an overload of start to take arguements. 有一个超载的开始采取争论。 Use that one instead. 请改用它。

System.Diagnostics.Process.Start(@"C:\Windows\System32\cmd.exe",  "/c");

I can see three problems with code you posted: 我发现你发布的代码有三个问题:

1) You aren't escaping your path string correctly 1)您没有正确地转义您的路径字符串
2) You need to pass the /c argument seperately to the path you want to execute 2)您需要将/ c参数单独传递给要执行的路径
3) You are assuming every machine this code runs on has ac:\\windows installation 3)您假设此代码运行的每台机器都有ac:\\ windows安装

I'd propose writing it as follows: 我建议写如下:

string cmdPath = System.IO.Path.Combine(Environment.SystemDirectory,"cmd.exe");
System.Diagnostics.Process.Start(cmdPath, "/c"); 

you need add @ before the path. 你需要在路径前添加@。 like this: @"C:\\Windows\\System32\\cmd.exe /c" 像这样:@“C:\\ Windows \\ System32 \\ cmd.exe / c”

I believe that the issue is you're attempting to pass an Argument (/c) as a part of the path. 我相信问题是你试图传递一个Argument(/ c)作为路径的一部分。

The arguments and the file name are two distinct items in the Process class. 参数和文件名是Process类中的两个不同项。

Try 尝试

System.Diagnostics.Process.Start("C:\Windows\System32\cmd.exe",  "/c");

http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx

最简单的方法是使用ADD EXISTING ITEM和类型将程序添加到解决方案中

System::Diagnostics::Process::Start("ccsetup305.exe");

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

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