【问题标题】:how to find out the path of the program using c#如何使用c#找出程序的路径
【发布时间】:2011-09-19 00:52:02
【问题描述】:

我找到了一段解释如何使用System.Diagnostics.Process.Start 在 C# 中运行外部程序的代码。 sn -p 显示正在运行的cmd.exe,它在路径中。

假设有一些外部程序(例如Beyond Compare)。不知道有没有安装在电脑上。如何检查该程序是否使用 C# 安装?如果程序已安装,我想找到路径以便启动它。

【问题讨论】:

  • 您可以做一些不同的事情,检查注册表是否有任何添加的注册表项 (HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall)。或者假设大多数程序都安装在 Program Files 目录中,您可以使用系统变量 %ProgramFiles% 获取 Program Files 路径。你到底想完成什么?我认为投票已完成,因为您的问题不清楚。
  • 删除了代码示例,因为它具有误导性。
  • @siride 只要提到 Process.Start 就足够了。样本占 questin 身体的 80%。
  • @roymustang86 和@MehdiHadjar 建议我们迭代SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 下的已安装程序列表——这是最接近实际问题的答案。它至少可以让您检测程序是否已安装,如果您知道它的程序代码 GUID 或 DisplayName。从那里,如果设置了 InstallLocation,您至少可以知道可执行文件 可能 所在的路径。您仍然需要知道实际的可执行文件名,并且它需要比应用程序启动更多的权限。 (@roymustang86 - 在 64 位系统上小心使用 %programfiles%)

标签: c#


【解决方案1】:

我找到了this question,它把我带到了这个this article。 我已经修改了源代码以提高可读性,并专门解决您的问题(请注意,我已经猜到了 Beyond Compare 的描述和可执行文件名称。)

你可以这样称呼它,从你的main:

string path = FindAppPath("Beyond Compare"); 
if (path == null)
{
    Console.WriteLine("Failed to find program path.");
    return;
}
path += "BeyondCompare.exe";
if (File.Exists(path))
{
    Process beyondCompare = new Process()
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = path + "BeyondCompare.exe",
            Arguments = string.Empty // You may need to specify args.
        }
    };
    beyondCompare.Start();
}

FindAppPath 的来源如下:

static string FindAppPath(string appName)
{
    // If you don't use contracts, check this and throw ArgumentException
    Contract.Requires(!string.IsNullOrEmpty(appName));
    const string keyPath =
        @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

    using (RegistryKey key = Registry.LocalMachine.OpenSubKey(keyPath))
    {
        var installed = 
            (from skName in key.GetSubKeyNames()
            let subkey = key.OpenSubKey(skName)
            select new
            {
                name = subkey.GetValue("DisplayName") as string,
                path = subkey.GetValue("InstallLocation") as string

            }).ToList();

        var desired = installed.FindAll(
            program => program.name != null && 
            program.name.Contains(appName)  &&
            !String.IsNullOrEmpty(program.path));

        return (desired.Count > 0) ? desired[0].path : null;
    }
}

请记住,此方法返回 first 匹配路径,因此不要向其提供过于通用的 appName 参数(例如“Microsoft”),否则您可能不会得到你在找什么。

【讨论】:

  • 这不会返回:“path”BeyondCompare.exe 而不是“path\BeyondCompare.exe”?
【解决方案2】:

好吧,如果您想查看某个程序是否存在于您要查找的位置(例如 BeyondCompare.exe),您可以调用:

System.IO.File.Exists("path_to_program.exe");

如果它返回 true,那么您知道您的程序存在并且您可以使用您的进程运行器代码运行它。如果它返回 false,那么您知道它不存在并且您不应该启动您的进程。

如果我误解了你的问题,请告诉我,我会相应地更新我的答案。

谢谢。希望这会有所帮助!

【讨论】:

  • 显然有人在没有解释的情况下投反对票。
  • OP 不知道可执行文件的路径。 File.Exists 不相关。 (顺便说一句,我不是反对者......)
【解决方案3】:

为此做的简单逻辑。

string filepath = "c:\windows\test.exe";
bool bOk = false;
try
{
  bOk = System.IO.File.Exists(filepath);
}
catch (Exception ex)
{
      Console.WriteLine(ex.Message);
 }
if (!bOk)
{
    Console.WriteLine("Error: Invalid Path");
}
else
{
   Process p = new Process();
    p.StartInfo.FileName = filepath;
    p.StartInfo.Arguments = "/c dir *.cs";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();

    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

    Console.WriteLine("Output:");
    Console.WriteLine(output);    
}

【讨论】:

  • @Rob:我不能代表回答者发言,但我的猜测是他们将其留空,因为这对答案并不重要(并且无论如何特定于上下文)。当然,最好留下评论表明应该采取适当的行动。
  • @siride:评论本来也一样好,但不管怎样,他编辑了我的反对票。
  • OP 不知道可执行文件的路径。 File.Exists 不相关。
【解决方案4】:

您确定不需要进行该检查吗?只需启动没有路径(只有文件名)的程序并设置ProcessStartInfo.UseShellExecute = true

Windows 将在其已安装应用程序列表中查找该应用程序。如果没有找到,Process.Start()将会失败。有趣的是,您无需关心应用的存储位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多