C # obtain the installation path of common software (call the Registry)

Source: Internet
Author: User

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;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.