0

Edit: I didn't mean to pinhole this question to a specific executable. I want this to work for any executable. If I was to type in the FileName into a run dialog, and the result is the "Windows cannot find ..." dialog, then I dont want to Start that process. Updated below...

I would like to execute 'an executable' in my program, but if the user doesn't have the executable installed, I want to run another process. How can I check BEFORE actually starting the visio process that visio is installed on the system? I do not want a popup "Windows cannot find "executable"...

Here's my code. But this does give the "Windows cannot find "executable".." error pop-up.

System.Diagnostics.Process myProc = new System.Diagnostics.Process();
myProc.StartInfo.FileName = "an executable.exe";
myProc.StartInfo.Arguments = "MyDoc.txt";
myProc.StartInfo.WorkingDirectory = "C:\MyFolder";

try
{
    if( !myProc.Start() )
    {
        myProc.StartInfo.FileName = "another process.exe";
        myProc.Start();
    }
}
catch (Exception ex)
{
    ...
}
Tizz
  • 820
  • 1
  • 15
  • 31
  • I don't know your specific situation - but could it not be better to determine if a `.vsd` handling program is installed (simpler than the other registry hacks presented as answers), and if not invoke the alternative behaviour? Or do you specifically need to split the world into Visio/Not Visio? – Damien_The_Unbeliever Nov 20 '12 at 18:26
  • I didn't mean to pinhole this question to a specific executable. I want this to work for any executable. If I was to type in the FileName into a run dialog, and the result is the "Windows cannot find ..." dialog, then I dont want to Start that process. Updated above... – Tizz Nov 20 '12 at 19:55

4 Answers4

2

I would do a lookup for HKEY_CLASSES_ROOT\Visio.Application in the registry. If it doesn't exist, no install. If it does exist, the CurVer sub key will give you something like Visio.Application.12 That tells you the DEFAULT version that is installed (might be others)

HKEY_CLASSES_ROOT\Visio.Application.12 Sub Key CLSID will give you a guid: {00021A20-0000-0000-C000-000000000046}

HKEY_CLASSES_ROOT\CLSID{00021A20-0000-0000-C000-000000000046} in turn will give you Sub Key "LocalServer32" Which will contain the path to the EXE.

C:\PROGRA~1\MICROS~4\Office12\VISIO.EXE /Automation

you could also check out this API as well GetBinaryType Function (Windows)

If you know the actual file path of where the program should be installed you could also do the following check

Well, if you're trying to see if a program exists where you're looking for it (like Visio.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.

MethodMan
  • 18,625
  • 6
  • 34
  • 52
1

First thing to do would be to locate visio.exe on the computer. This would probably mean reading the install location from the registry and then verifying that visio.exe exists at that location. If you can't find the proper registry key or the file does not exist, it is reasonable to assume Visio is not installed.

Robert Jeppesen
  • 7,837
  • 3
  • 35
  • 50
1

tldr; Something else causes the "Windows cannot find .." dialog - what does the Exception handling code look like?

On a system without Visio the code

System.Diagnostics.Process myProc = new System.Diagnostics.Process();
myProc.StartInfo.FileName = "visio.exe";
myProc.StartInfo.Arguments = "MyVisioDoc.vsd";
myProc.StartInfo.WorkingDirectory = "C:\\MyVisioFolder"; // fixed literal
myProc.Start();

results in the helpful Exception being thrown

Win32Exception: The system cannot find the file specified

While this will result in Visio running if it is found, if Visio isn't found, there is no inherent reason why a dialog message box is shown. That's not normal behavior.

Now, if launching via MyVisioDoc.vsd directly (e.g. using the Open Verb) then yes, that is passed off to the windows system and may, if .vsd files are associated with a missing Visio application, result in such a dialog. However, this a different scenario than that presented in this post.

0

You can check this in the registry.

Try this code:

  public static class ProgramHelper
    {
        public static bool IsProgramInstalled(string displayName, bool x86Platform)
        {
            string uninstallKey = x86Platform
                                      ? @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
                                      : @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";

            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
            {
                if(rk != null)
                    foreach (string skName in rk.GetSubKeyNames())
                    {
                        using (RegistryKey sk = rk.OpenSubKey(skName))
                        {
                            if (sk != null && sk.GetValue("DisplayName") != null &&
                                sk.GetValue("DisplayName").ToString().ToUpper().Equals(displayName.ToUpper()))
                            {
                                return true;
                            }

                        }
                    }
            }
            return false;
        }
    }

And you can invoke this like this:

ProgramHelper.IsProgramInstalled("Microsoft Visio Standard 2010", true);
ProgramHelper.IsProgramInstalled("Microsoft Visio Premium 2010", true);
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65