我有以下的瑣碎的C#應用程序,它只是試圖啓動「jconsole.exe」,這在我的機器上位於C:\程序\ jdk16 \ bin中。的Process.Start()和PATH環境變量
using System;
using System.Diagnostics;
namespace dnet {
public class dnet {
static void Main(string[] args) {
try {
Process.Start("jconsole.exe");
Console.WriteLine("Success!");
} catch (Exception e) {
Console.WriteLine("{0} Exception caught.", e);
}
}
}
}
如果我的PATH環境變量設置爲
c:\windows;c:\windows\sytem32;c:\programs\jdk16\bin
它完美的作品。但是,如果PATH環境變量設置爲
c:\windows;c:\windows\sytem32;c:\\programs\jdk16\bin
(注意兩者之間的反斜槓「C:」和「方案」),它沒有用win32異常。
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at dnet.dnet.Main(String[] args)
有趣的是,當我運行.NET程序,並得到異常同一命令提示符下,我可以簡單地輸入「jconsole.exe」,程序將啓動。 Windows在PATH中使用雙反斜槓查找可執行文件似乎沒有問題,但是Process.Start()卻可以。
爲什麼是額外的反斜槓在PATH造成的問題,我怎樣才能解決這個問題呢?我不知道我要調用的可執行文件位於運行時,所以我寧願依賴PATH變量。
有兩種方法來啓動一個EXE,你正在測試兩種方式。您的應用程序使用ShellExecuteEx(),命令行解釋程序使用CreateProcess()。你可以玩ProcessStartInfo.UseShellExecute屬性。沒有太多關於他們如何解釋PATH環境變量的問題,你知道如何解決這個問題。 –