I am reading the path for the latest version of Word installed from the registry, however even though the key exists, it does not return the path.
private string GetWordInstallPath()
{
int[] versions = new int[]
{
16, // 2016
15, // 2013
14, // 2010
12, // 2007
11, // 2003
10, // XP
9, // 2000
8, // 98
7 // 97
};
string path = "";
string hklm = @"SOFTWARE\Microsoft\Office\{0}.0\Word\InstallRoot";
foreach (int ver in versions)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(string.Format(hklm, ver), false);
if (key != null)
{
path = key.GetValue("Path", "").ToString();
key.Close();
break;
}
}
return path;
}
}
In my case, I have Office 2016 installed, and I do not have Office 2013 installed, however the above method returns :
C:\Program Files\Microsoft Office 15\root\Office 15\
As you can see here, there is no such entry for SOFTWARE\Microsoft\Office\15.0\Word\InstallRoot
, however there is a path for Office 2016.
Why is 'key' null when reading the path for 16, but returns a path for a non-existent installation in a folder that doesn't even exist on my system. The expected result from the above code is to loop through the values in versions
checking for the subkey InstallPath for word, and on the first match, return the path for that version as it would be the most current version of word installed.
That's kind of impossible to do when it is pulling values for non-existent installs and NOT returning the existing install. This also needs to be done without using Interop.
The method above is from mangling together bits and pieces from here, here, here, and to detect that any version is installed I used code from here (as follows fyi)
private bool IsWordInstalled()
{
Type officeType = Type.GetTypeFromProgID("Word.Application");
return (officeType == null) ? false : true;
}