Many software installation locations are different, but they basically write their names and program paths in the same location in the registry. This location is:
HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ app paths
So you only need to find the software path here.
You can add the correct file name you know, that is, its name in the registry. For example, Office Word is called winword in the registry.
The complete path of the EXE is obtained. If you want a folder, use path. getdirectoryname ().
Public Enum Softwares
{
// The names are the same with the registry names.
// You can add any software exists in the "Regedit" Path:
// HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ app paths
Excel, // Office EXCEL
Winword, // Office Word
Msaccess, // office access
Powerpnt, // Office PowerPoint
Outlook, // office Outlook
Infopath, // office infopath
Mspub, // Office Publisher
Visio, // office Visio
Iexplore, // IE
ITunes // Apple iTunes
//.........
}
Public class softwareoperator
{
// When you do not want to use string name, then use the enum instead
Public static bool trygetsoftwarepath (softwares softname, out string path)
{
Return trygetsoftwarepath (softname. tostring (), Out path );
}
Public static bool trygetsoftwarepath (string softname, out string path)
{
String strpathresult = string. empty;
String strkeyname = ""; // "(default)" key, which contains the intalled path
Object objresult = NULL;
Microsoft. win32.registryvaluekind regvaluekind;
Microsoft. win32.registrykey regkey = NULL;
Microsoft. win32.registrykey regsubkey = NULL;
Try
{
// Read the key
Regkey = Microsoft. win32.registry. localmachine;
Regsubkey = regkey. opensubkey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ app paths \" + softname. tostring () + ". EXE", false );
// Read the path
Objresult = regsubkey. getvalue (strkeyname );
Regvaluekind = regsubkey. getvaluekind (strkeyname );
// Set the path
If (regvaluekind = Microsoft. win32.registryvaluekind. String)
{
Strpathresult = objresult. tostring ();
}
}
Catch (system. Security. securityexception ex)
{
Throw new system. Security. securityexception ("You have no right to read the registry! ", Ex );
}
Catch (exception ex)
{
Throw new exception ("reading registry error! ", Ex );
}
Finally
{
If (regkey! = NULL)
{
Regkey. Close ();
Regkey = NULL;
}
If (regsubkey! = NULL)
{
Regsubkey. Close ();
Regsubkey = NULL;
}
}
If (strpathresult! = String. Empty)
{
// Found
Path = strpathresult;
Return true;
}
Else
{
// Not found
Path = NULL;
Return false;
}
}
}