0

I am trying to create a quick launcher application and to run an application for example I use

 Process.Start("chrome.exe");

However, I want the application to check if that app (chrome) exist or not and if it wasn't found then it would prompt the user if they want to change the path to find chrome and if they press yes a file explorer would show and they can choose where the file exists. Basically, I am trying to make it a dynamic path instead of a fixed path. Any suggestions?

EDIT: The difference between this question and the linked is that I am more looking on finding how to make the path dynamic and not just the existence of a file, and if it was not found then a search window will show up to let the user where the file exists and therefore, change the path in code.

  • Use `System.IO.File` to check if the file exists `if(!File.Exists([File Path])) { // do something } else { // do something else }`. As for prompts I suggest checking this link https://stackoverflow.com/questions/5427020/prompt-dialog-in-windows-forms – Bargros Dec 17 '18 at 10:52
  • Does chrome start if you write chrome.exe in the command prompt? – Magnus Dec 17 '18 at 12:48
  • @Magnus Only if you have "Start" before it, for example, "start chrome.exe" then yes it would start in command prompt – Khalid altunaiji Dec 17 '18 at 18:35

2 Answers2

2

So if you want to know if an application is installed in windows you can iterate the Uninstallable apps in the registry below SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

However some application just have an executable are are not "installed".

void Main()
{
    if (GetInstalledApplications().Any(a => a == "Google Chrome"))
    {
        //Chrome is installed
    }
}

public IEnumerable<string> GetInstalledApplications()
{
    string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    using (var key = Registry.LocalMachine.OpenSubKey(registry_key))
    {
        foreach (var subkey_name in key.GetSubKeyNames())
        {
            using (var subkey = key.OpenSubKey(subkey_name))
            {
                yield return (string)subkey.GetValue("DisplayName");
            }
        }
    }
}

If you want to get an installed application by default executable file name (and be able to launch it) you can iterate the values in SOFTWARE\Microsoft\Windows\CurrentVersion\App paths

void Main()
{
    var appPath = GetInstalledApplications()
                     .Where(p => p != null && p.EndsWith("Chrome.exe", StringComparison.OrdinalIgnoreCase))
                     .FirstOrDefault();
    if(appPath != null)
    {
        //Launch
    }
}

public IEnumerable<string> GetInstalledApplications()
{
    string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App paths";
    using (var key = Registry.LocalMachine.OpenSubKey(registry_key))
    {
        foreach (var subkey_name in key.GetSubKeyNames())
        {
            using (var subkey = key.OpenSubKey(subkey_name))
            {
                yield return (string)subkey.GetValue("");
            }
        }
    }
}

With this solution you could use a search drop-down with the result of GetInstalledApplications to help the user to select the correct program instead of relying on them knowing the correct filename.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Magnus
  • 45,362
  • 8
  • 80
  • 118
0

The below code is working. Please check the same. I have created a sample windows application and test the same. It is working.

        Process process = new Process();
        try
        {
            // Configure the process using the StartInfo properties.
            process.StartInfo.FileName = "chrome1.exe";
            process.StartInfo.Arguments = "-n";
            process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            process.Start();
            process.WaitForExit();// Waits here for the process to exit.
        }
        catch (Win32Exception ex)
        {
            if (ex.Message.Equals("The system cannot find the file specified"))
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    process.StartInfo.FileName = openFileDialog1.FileName;
                    process.Start();
                }
            }
        }
A. Gopal Reddy
  • 370
  • 1
  • 3
  • 16
  • This is working great for me so far, however, after choosing the correct file/process from the file dialog I want it to be saved permanently instead of doing it every time. Any idea how to do that? – Khalid altunaiji Dec 17 '18 at 18:09
  • Save your content/info to a xml file. So, that it is easy to extract also – A. Gopal Reddy Dec 18 '18 at 04:43