C # obtain the program installation directory

Source: Internet
Author: User

To start a local program on a webpage, you need to write the command to the Registry and call the command on the webpage.

First, create a registry file. Reg format for registration information. Take the page to start the notepad ++ program as an example.
Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\Webshell][HKEY_CLASSES_ROOT\Webshell\DefaultIcon][HKEY_CLASSES_ROOT\Webshell\shell][HKEY_CLASSES_ROOT\Webshell\shell\open][HKEY_CLASSES_ROOT\Webshell\shell\open\command]@="\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\""

 

When HTML code is called on the page, an external request prompt is displayed. You can directly start the application.
<A href = 'webshell: // '> Start a local program using webshell </a>
-------------------------------------- Split line ------------------------------------------- in the project, the registration script does not allow users to register by themselves, so you need to execute the registration information in the program and use C.
    private static void Main(string[] args)        {       var notepadPath = @"C:\Program Files (x86)\Notepad++\notepad++.exe";           CreateRegistryKey("Webshell", notepadPath);                       }
        public static void CreateRegistryKey(string shell, string path)        {            RegistryKey key = Registry.ClassesRoot;            key.CreateSubKey(shell);            key.CreateSubKey(string.Format(@"{0}\DefaultIcon", shell));            key.CreateSubKey(string.Format(@"{0}\shell", shell));            key.CreateSubKey(string.Format(@"{0}\shell\open", shell));            var command = key.CreateSubKey(string.Format(@"{0}\shell\open\command", shell));            command.SetValue("", path);            key.Close();        }

In a real user environment, you cannot determine where a program is installed, so the installation directory of the program cannot be fixed.

Baidu: Find a method. Some programs can be found, but some programs cannot be found. I don't know why?

/// <Summary> /// obtain the execution directory of a single program // </Summary> /// <Param name = "processname"> </param> /// <returns> </returns> Public static string getpath (string processname) {var process = process. getprocessesbyname (processname); var Path = string. empty; // program path foreach (var p in process. where (P => P. main1_whandle! = Intptr. Zero) {Path = P. mainmodule. filename; break;} return path ;}
Private Static void main (string [] ARGs ){
VaR notepadpath = getpath ("Notepad ++"); console. writeline ("program name: notepad ++ \ n installation directory:" + notepadpath); createregistrykey ("webshell", notepadpath );}

Baidu again, find the method to get the installation directory of all programs, only the obtained installation path, not the complete available path.

/// <Summary> /// obtain the installation directory of all programs /// </Summary> Public static void getallprocess () {const string uninstall = @ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall"; using (VAR registrykey = registry. localmachine. opensubkey (uninstall, false) {If (registrykey! = NULL) // determine whether the object exists {foreach (VAR keyname in registrykey. getsubkeynames () // traverses the string array of the subitem name {using (var key = registrykey. opensubkey (keyname, false) // traverses the subitem node {If (key! = NULL) {var softwarename = key. getvalue ("displayname ",""). tostring (); // obtain the software name var installlocation = key. getvalue ("installlocation ",""). tostring (); // obtain the installation path if (! String. isnullorempty (installlocation) {console. writeline (softwarename); console. writeline (installlocation); console. writeline ();}}}}}}}

 

Finally, I would like to ask you a question: how to obtain the installation path of an application, only through the program name?

 

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.