C# Process.Start() 出错 -- 系统找不到指定的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2369119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error in Process.Start() -- The system cannot find the file specified
提问by SO User
I am using the following code to fire the iexplore process. This is done in a simple console app.
我正在使用以下代码来触发 iexplore 进程。这是在一个简单的控制台应用程序中完成的。
public static void StartIExplorer()
{
var info = new ProcessStartInfo("iexplore");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
string password = "password";
SecureString securePassword = new SecureString();
for (int i = 0; i < password.Length; i++)
securePassword.AppendChar(Convert.ToChar(password[i]));
info.UserName = "userName";
info.Password = securePassword;
info.Domain = "domain";
try
{
Process.Start(info);
}
catch (System.ComponentModel.Win32Exception ex)
{
Console.WriteLine(ex.Message);
}
}
The above code is throwing the error The system cannot find the file specified
. The same code when run without specifying the user credentials works fine. I am not sure why it is throwing this error.
上面的代码抛出错误The system cannot find the file specified
。在不指定用户凭据的情况下运行相同的代码工作正常。我不确定为什么会抛出此错误。
Can someone please explain?
有人可以解释一下吗?
采纳答案by Jojo Sardez
Try to replace your initialization code with:
尝试将您的初始化代码替换为:
ProcessStartInfo info
= new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore.exe");
Using non full filepath on Process.Start
only works if the file is found in System32 folder.
Process.Start
只有在 System32 文件夹中找到文件时,才使用非完整文件路径。
回答by Niels Schmidt
You can use the folowing to get the full path to your program like this:
您可以使用以下内容获取程序的完整路径,如下所示:
Environment.CurrentDirectory
回答by Fraser
You can't use a filename like iexplore
by itself because the path to internet explorer isn't listed in the PATH
environment variable for the system or user.
您不能单独使用文件名iexplore
,因为 Internet Explorer 的路径未列在PATH
系统或用户的环境变量中。
However any path entered into the PATH
environment variable allows you to use just the file name to execute it.
但是,输入到PATH
环境变量中的任何路径都允许您仅使用文件名来执行它。
System32
isn't special in this regard as anydirectory can be added to the PATH
variable. Each path is simply delimited by a semi-colon.
System32
在这方面没有什么特别之处,因为任何目录都可以添加到PATH
变量中。每条路径都由分号简单地分隔。
For example I have c:\ffmpeg\bin\
and c:\nmap\bin\
in my path environment variable, so I can do things like new ProcessStartInfo("nmap", "-foo")
or new ProcessStartInfo("ffplay", "-bar")
例如,我在我的路径环境变量中有c:\ffmpeg\bin\
and c:\nmap\bin\
,所以我可以做类似new ProcessStartInfo("nmap", "-foo")
或 new ProcessStartInfo("ffplay", "-bar")
The actual PATH
variable looks like this on my machine.
实际PATH
变量在我的机器上看起来像这样。
%SystemRoot%\system32;C:\FFPlay\bin;C:\nmap\bin;
As you can see you can use other system variables
, such as %SystemRoot%
to build and construct paths in the environment variable.
如您所见,您可以使用 other system variables
,例如%SystemRoot%
在环境变量中构建和构造路径。
So - if you add a path like "%PROGRAMFILES%\Internet Explorer;" to your PATH
variable you will be able to use ProcessStartInfo("iexplore");
所以 - 如果你添加一个像“%PROGRAMFILES%\Internet Explorer;”这样的路径 到您的PATH
变量,您将能够使用ProcessStartInfo("iexplore");
If you don't want to alter your PATH
then simply use a system variable such as %PROGRAMFILES%
or %SystemRoot%
and then expand it when needed in code. i.e.
如果你不想改变你的,PATH
那么只需使用一个系统变量,例如%PROGRAMFILES%
or %SystemRoot%
,然后在代码中需要时扩展它。IE
string path = Environment.ExpandEnvironmentVariables(
@"%PROGRAMFILES%\Internet Explorer\iexplore.exe");
var info = new ProcessStartInfo(path);
回答by Macke
Also, if your PATH's dir is enclosed in quotes, it will work from the command prompt but you'll get the same error message
此外,如果您的 PATH 目录包含在引号中,它将在命令提示符下工作,但您将收到相同的错误消息
I.e. this causes an issue with Process.Start() not finding your exe:
即这会导致 Process.Start() 找不到您的 exe 的问题:
PATH="C:\my program\bin";c:\windows\system32
Maybe it helps someone.
也许它可以帮助某人。
回答by 0xBADF00D
I had the same problem, but none of the solutions worked for me, because the message The system cannot find the file specified
can be misleading in some special cases.
我遇到了同样的问题,但没有一个解决方案对我有用,因为The system cannot find the file specified
在某些特殊情况下该消息可能会产生误导。
In my case, I use Notepad++in combination with the registry redirectfor notepad.exe. Unfortunately my path to Notepad++ in the registry was wrong.
就我而言,我将Notepad++与notepad.exe的注册表重定向结合使用。不幸的是,我在注册表中的 Notepad++ 路径是错误的。
So in fact the message The system cannot find the file specified
was telling me, that it cannot find the application (Notepad++) associated with the file type(*.txt), not the file itself.
因此,实际上该消息The system cannot find the file specified
告诉我,它找不到与文件类型 (*.txt) 关联的应用程序 (Notepad++),而不是文件本身。