我使用以下代码来触发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);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码抛出错误The system cannot find the file specified.运行时相同的代码而不指定用户凭据可以正常工作.我不确定为什么会抛出这个错误.
有人可以解释一下吗?
Joj*_*dez 58
尝试用以下代码替换初始化代码:
ProcessStartInfo info
= new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore.exe");
Run Code Online (Sandbox Code Playgroud)
Process.Start仅当在System32文件夹中找到该文件时,才使用非完整文件路径.
Fra*_*ser 13
您不能单独使用文件名iexplore,因为Internet Explorer的路径未列在PATH系统或用户的环境变量中.
但是,输入到PATH环境变量中的任何路径都允许您仅使用文件名来执行它.
System32在这方面并不特别,因为任何目录都可以添加到PATH变量中.每条路径都只用分号分隔.
例如,我c:\ffmpeg\bin\和c:\nmap\bin\我的路径环境变量,所以我可以做new ProcessStartInfo("nmap", "-foo")或 像new ProcessStartInfo("ffplay", "-bar")
实际PATH变量在我的机器上看起来像这样.
%SystemRoot%\system32;C:\FFPlay\bin;C:\nmap\bin;
Run Code Online (Sandbox Code Playgroud)
如您所见,您可以使用其他system variables,例如%SystemRoot%在环境变量中构建和构造路径.
所以 - 如果你添加一个像"%PROGRAMFILES%\ Internet Explorer"这样的路径 你PATH可以使用你的变量ProcessStartInfo("iexplore");
如果您不想改变它,PATH那么只需使用系统变量(如%PROGRAMFILES%或)%SystemRoot%,然后在需要时在代码中展开它.即
string path = Environment.ExpandEnvironmentVariables(
@"%PROGRAMFILES%\Internet Explorer\iexplore.exe");
var info = new ProcessStartInfo(path);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99527 次 |
| 最近记录: |