Yet another interesting MSDN forum post I answered.  A user asked how they could programmatically determine the installation directory of SharePoint.  After multiple searches on the web, etc. I stumbled on the answer….read the registry!

Here is some sample code (i havent actually tested this code but the logic is sound):

using Microsoft.Win32;
...
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Office Server\\12.0");
if (masterKey == null)
{
   Console.WriteLine ("Null Masterkey!");
}
else
{
   Console.WriteLine ("MyKey = {0}", masterKey.GetValue ("InstallPath"));
}
masterKey.Close();

 

the only issue I see here is that depending your farm (a 5 server farm for example), is if you install SharePoint in a different directory on each web front end.  In that case, depending on what server the request gets processed, you may get a different location that if you run the same request on a different WFE where the install directory was different. 

Honeslty I've never run into a case where the install directory was different on each WFE since most WFE's mirror each other, just thought I would throw it out there to you as well as the fact that it would just be bad practice to have the WFE’s use different install directories.

Another user responded to the post and said you could do it using the SPUtility class’s GetGenericSetupPath method, which seems a bit more logical and using the SharePoint API.

using Microsoft.SharePoint.Utilities;
...
string featurePath = SPUtility.GetGenericSetupPath("12");

Posted in:   Tags: , ,
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017 Tony Testa's World