开发者

how to find out the path of the program using c#

I found a snippet of code explaining how to use System.Diagnostics.Process.Start to run an external program in C#. The snippet shows running cmd.exe, which is in the path.开发者_开发知识库

Let's assume that there is some external program (Beyond Compare for example). I don't know if it is installed on the PC. How can I check if this program is installed using C#? If the program is installed, I would like to find the path so that I can launch it.


I found this question, which directed me to this this article. I've modified the source for readability, and to solve your problem specifically (note that I've guessed the description and executable name of Beyond Compare.)

You can call it like this, from your 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();
}

The source for FindAppPath follows:

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;
    }
}

Keep in mind that this method returns the first matching path, so don't feed it an appName argument that's too generic (eg. "Microsoft") or you probably won't get what you're looking for.


Well, if you're trying to see if a program exists where you're looking for it (like BeyondCompare.exe), you can just use a call to:

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

If it returns true, then you know your program exists and you can run it with your process runner code. If it returns false, then you know it's not there and you shouldn't launch your process.

If I'm misunderstanding your question, please let me know and I'll update my answer accordingly.

Thanks. Hope this helps!


Simple logic to do for this.

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);    
}


Are you sure you don't need to make that check. Simply start the program without path (only the filename) and set ProcessStartInfo.UseShellExecute = true.

Windows will look for the app in its list of installed app. If it doesn't find it, Process.Start()will fail. The interesting thing is that you never had to care about where the app is stored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜