0

By using following code[C#], we can know the path where googlechrome is installed. But, this code first starts chrome.exe, then takes its path. My question is without starting chrome.exe, how can I know the path?

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "chrome.exe";
        string argmnt = @"-g";
        startInfo.Arguments = argmnt;

        String path = null;
        try
        {
            Process p = Process.Start(startInfo);
            path = p.MainModule.FileName;
            p.Kill();
        }
        catch (Exception e)
        {
            return String.Empty;
        }
4

3 回答 3

0

Parse the PATH environment variable and enumerate through all of the directories looking for that exe. This is essentially what the OS does when you call the p.Start method.

于 2012-04-15T04:02:46.480 回答
0

首先从 Microsoft 下载一个名为“Process Monitor”的工具:Microsoft SysInternals 提供的 ProcessMonitor,然后启动 Chrome 以找出它的位置。

提示:“进程监视器”实时监控硬盘驱动器、注册表和线程/进程信息,并允许您保存它捕获的跟踪。

在此处输入图像描述

一个。打开进程监视器,它将开始跟踪信息。

湾。通过单击放大镜工具栏按钮(它的开/关跟踪按钮)停止跟踪。

C。然后单击清除跟踪按钮以清除跟踪。

d。启动 Chrome,一旦打开,搜索 Chrome.exe,它会告诉你文件路径。

于 2012-04-15T05:26:08.487 回答
0

使用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;
        }
    }
}
于 2012-04-15T05:34:55.693 回答