使用WHERE命令和chrome.exe
作爲參數。這將告訴你將被shell加載的可執行文件的路徑。
你只需要回讀命令的輸出。
這與您當前的版本一樣,假定可執行文件位於系統PATH中。
下面是一些代碼,您可以根據自己的需要進行調整。它基本上包裝了WHERE命令(順便說一句是一個可執行文件,所以WHERE WHERE
將顯示其路徑)。
using System;
using System.Diagnostics;
public sealed class WhereWrapper
{
private static string _exePath = null;
public static int Main(string[] args) {
int exitCode;
string exeToFind = args.Length > 0 ? args[0] : "WHERE";
Process whereCommand = new Process();
whereCommand.OutputDataReceived += Where_OutputDataReceived;
whereCommand.StartInfo.FileName = "WHERE";
whereCommand.StartInfo.Arguments = exeToFind;
whereCommand.StartInfo.UseShellExecute = false;
whereCommand.StartInfo.CreateNoWindow = true;
whereCommand.StartInfo.RedirectStandardOutput = true;
whereCommand.StartInfo.RedirectStandardError = true;
try {
whereCommand.Start();
whereCommand.BeginOutputReadLine();
whereCommand.BeginErrorReadLine();
whereCommand.WaitForExit();
exitCode = whereCommand.ExitCode;
} catch (Exception ex) {
exitCode = 1;
Console.WriteLine(ex.Message);
} finally {
whereCommand.Close();
}
Console.WriteLine("The path to {0} is {1}", exeToFind, _exePath ?? "{not found}");
return exitCode;
}
private static void Where_OutputDataReceived(object sender, DataReceivedEventArgs args) {
if (args.Data != null) {
_exePath = args.Data;
}
}
}
您是否曾經解決過這個問題? – harpo 2012-04-20 15:23:39