Where are you Outlook???


So I got this friend of mine who works for MS IT and was curious to find Outlook’s install location programmatically irrespective of 32/64 bit installation of OS or Outlook.

First thought was registry, but that gets little messy.. you go to HKCR & then parse through it to find the location of Outlook.exe… I didn’t like that much, and that was not sure shot way… I could delete the Outlook.exe and it will still point me to the same path. You might ask why would someone delete the Outlook.exe and leaving other registry entries? My answer is because you can and then the registry solution will break.

So why not ask the Outlook itself where are you buddy? Nice, you would say, if only he can speak… yes he can!!! Here is how its done in C#

I am using Reflection to create an object to Outlook.Application. You can use this to create an object of any COM Object though. Whenever you create the object to a COM server inside EXE, its EXE is launched if not running already. So all you need to do is use System.Diagnostics.Process class to get its Process Path.

using System;
using System.Reflection;
using System.Diagnostics;

namespace OutlookLocationFinder
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Type objType = null;
            object comObject = null;
            string version = string.Empty;
            Process[] p = null;

            try
            {

                // Create an object to Outlook.Application , replace with your COM Server's ProgID
                objType = System.Type.GetTypeFromProgID("Outlook.Application");

                // Activate the object
                comObject = System.Activator.CreateInstance(objType);

                // This is Outlook specific, I am calling the member property to get the version of Outlook
                version = (string)objType.InvokeMember("Version",
                               BindingFlags.InvokeMethod, null, comObject, null);
            }
            catch
            {
                Console.Write("Unable to create the object of type \"Outlook.Application\"");
            }

            // Whenever you create the object to a COM server inside EXE, its EXE is launched if not running already
            // Now, just get hold of the Process by its name
            p = Process.GetProcessesByName("Outlook");
            
            // Beware there could be multiple instances running of the same application, we just care about first one.
            if (null != p && p.Length > 0)
                Console.WriteLine("Outlook ({0}) is launched from: {1}", version, p[0].MainModule.FileName);
            else
                Console.WriteLine("Outlook does not seem to be installed properly on this machine, could not find process running on local machine");

            Console.Write("Press any key to exit.");
            Console.ReadKey(false);
        }
    }
}
Happy Coding / Debugging !!!
 
Skip to main content