无法找到特定文件
Unable to find particular file
当我在我的程序中尝试启动特定的 exe 文件 "nvidia-smi.exe"(NVIDIA 系统管理界面程序)时,我收到错误 "System.ComponentModel.Win32Exception. The system cannot find the file specified"
string directoryPath = "C:\";
string fileName = "nvidia-smi.exe";
Console.WriteLine(System.IO.File.Exists(directoryPath + fileName)); //true
proc.StartInfo.WorkingDirectory = directoryPath;
proc.StartInfo.FileName = fileName;
proc.Start(); //Error. The system cannot find the file specified
但同时我可以:
1) 从同一目录启动其他文件(exe、bat 等)
2) 成功执行我需要的文件 "nvidia-smi.exe" 如果将它重新定位到我的项目目录并且不使用 属性 "proc.StartInfo.WorkingDirectory".
-----------------答案是(感谢帮助!)-----------------
你需要这个:
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.WorkingDirectory = "C:\";
proc.StartInfo.FileName = "nvidia-smi.exe";
或者这个:
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "C:\nvidia-smi.exe";
如果将 proc.StartInfo.UseShellExecute
设置为 true,WorkingDirectory
的行为将如您所愿。否则,您将必须为 FileName
指定绝对路径,或者确保您的可执行文件位于您的环境路径中。
相关文档:
当我在我的程序中尝试启动特定的 exe 文件 "nvidia-smi.exe"(NVIDIA 系统管理界面程序)时,我收到错误 "System.ComponentModel.Win32Exception. The system cannot find the file specified"
string directoryPath = "C:\";
string fileName = "nvidia-smi.exe";
Console.WriteLine(System.IO.File.Exists(directoryPath + fileName)); //true
proc.StartInfo.WorkingDirectory = directoryPath;
proc.StartInfo.FileName = fileName;
proc.Start(); //Error. The system cannot find the file specified
但同时我可以:
1) 从同一目录启动其他文件(exe、bat 等)
2) 成功执行我需要的文件 "nvidia-smi.exe" 如果将它重新定位到我的项目目录并且不使用 属性 "proc.StartInfo.WorkingDirectory".
-----------------答案是(感谢帮助!)-----------------
你需要这个:
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.WorkingDirectory = "C:\";
proc.StartInfo.FileName = "nvidia-smi.exe";
或者这个:
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "C:\nvidia-smi.exe";
如果将 proc.StartInfo.UseShellExecute
设置为 true,WorkingDirectory
的行为将如您所愿。否则,您将必须为 FileName
指定绝对路径,或者确保您的可执行文件位于您的环境路径中。
相关文档: