. NET registry

Source: Internet
Author: User

1. How C # operates on the registryWe'll use it next. NET Managed language C # Registry operations, the main contents include: the creation of registry keys, open and Delete, the creation of key values (set values, modifications), read and delete, determine whether the registry key exists, determine whether the key value exists. Preparation: 1: To manipulate the registry, we have to introduce the necessary namespaces:  using Microsoft.Win32; This namespace contains many registry-related classes, enough for us to use ~ ~ 2: The namespace provides a class: RegistryKey uses it to navigate to the branch at the very beginning of the registry: Classesroot,currentuser,users,localmachine, Currentconfig such as: RegistryKey key = registry.localmachine;3: In the process of operation involves sub-branches, to use \ \ To drill down, a single \ will error! 4: Finally, close () to invoke the RegistryKey object closes the modification to the registry ~~~5: Here are our examples under the LocalMachine branch, please note.    A: Create a C # registry key, open and delete 1: Create a registry key using the RegistryKey CreateSubKey () method. such as:  registrykey key = Registry.localmachine; RegistryKey software = key. CreateSubKey ("Software\\test");//Create a new registry key named Test under HKEY_LOCAL_MACHINE\Software. It does not affect if it already exists!  2: Open the Open registry key using the RegistryKey OpenSubKey () method. For example: Note that if the registry key does not exist, this call to this method throws an exception  registrykey key = Registry.localmachine; RegistryKey software = key. OpenSubKey ("Software\\test", true);//Note that the method can also have a Boolean parameter followed by true to indicate that it can be written.  3: Deleting a registry key is primarily used with the RegistryKey DeleteSubKey () method. such as:  registrykey key = Registry.localmachine;key. DeleteSubKey ("software\\tEst ", true); The method has no return value and can be called directly by key. Close ();   Note that if the registry key does not exist, this call to this method throws an exception    two: Key value creation (set value, modify), read and delete 1: Create (set value, modify) The creation and modification of key values are mainly used in the RegistryKey SetValue () method   Copy code RegistryKey key = Registry.localmachine; RegistryKey software = key. OpenSubKey ("Software\\test", true); The item must already exist software. SetValue ("Test", "blog Park");//Create a key value named "Test" with the value "blog Park" under HKEY_LOCAL_MACHINE\Software\Test. If the key value already exists, it will be modified to replace the original key value, and if it does not exist, the key value is created. Note: SetValue () also has a third parameter, mainly for setting the type of the key value, such as: string, Binary, DWORD, etc. ~ ~ The default is the string. such as://software. SetValue ("Test", "0", Registryvaluekind.dword); Binary information key.close (); Copy code  2: Read   Copy code string info = ""; RegistryKey Key; Key = Registry.localmachine;myreg = Key.opensubkey ("Software\\test");//Myreg = Key.opensubkey ("Software\\test", True) ; info = Myreg. GetValue ("Test"). ToString (); Myreg. Close (); Copy code info results for: Blog Park   3: delete  registrykey Delkey = Registry.LocalMachine.OpenSubKey ("software\\ Test ", True);d elkey.deletevalue (" test ");d Elkey.close (); The careful reader may find a second example in which the OpenSubKey () method parameter differs from other examples. If you want to modify the key values, including creating,Set, delete the key value and so on to add a Boolean parameter after the method, set to True, indicating writable can be changed, if just read the key value can not add, at this time can write off, you can not write the value (of course, you have to add also can be true)! Readers also mentioned the problem of reading and writing the default key values, mainly in the settings, read the method of the key name is the default key value operation. such as: Software. SetValue ("", "blog Park"); In HKEY_LOCAL_MACHINE\Software\Test, modify the value of the default key value to "blog Park". Read similar! In addition, the default key value can not be deleted, so do not use the DeleteValue () method to delete, will throw an exception!    Three: Determine if the registry key exists   copy code private bool Isregedititemexist ()  {     string [] subkeynames;      RegistryKey hkml = registry.localmachine;      RegistryKey software = hkml. OpenSubKey ("Software");     //registrykey software = hkml. OpenSubKey ("Software", true);      Subkeynames = software. Getsubkeynames ();     //Get a sequence of the names of all subkeys under the key and pass them to the predetermined array      foreach (string keyName in Subkeynames)      //Traverse entire array      {         if (KeyName = = "Test")         //Determine the name of the subkey          {             Hkml. Close ();              return true;         }     }      HKML. Close ();      return false;   Copy Code    Four: Determine if key value exists   copy code private bool Isregeditkeyexit () {  string[] subkeynames;  RegistryKey hkml = registry.localmachine;  RegistryKey software = hkml. OpenSubKey ("Software\\test"); //registrykey software = hkml. OpenSubKey ("Software\\test", true);  subkeynames = software. GetValueNames (); //Gets the sequence of names of all key values under the key and passes to the predetermined array in   foreach (string keyName  in subkeynames)   {    if (KeyName = =   "Test")//determine the name of the key value     {        HKML. Close ();        return true;   }        }  hkml. Close ();  return false;} External links

2. Boot registry Location

It is easy to package the project with the VS-pack module, and we can also start the software by operating the registry when we install the deployment. The implementation is as follows: To create an installation deployment this part of the section does not have to say, after the installation of the deployment project, the right mouse button installation of the project--View--the registry, to enable the software to run, you can hkey_current_user\software\microsoft\ Windows\currentversion\run add key value to save the software catalog. Here we add the above items in turn, and then add the key value in run, the key name can be the starting point, value to fill the physical path of the software. The physical path is determined by the customer in the deployment, how do we get it? Here we can use [TARGETDIR] to get the client's chosen path, plus the software's startup file name. For example, the name of the software startup file is Client.exe, then value is: [Targetdir]client.exe. Build the Setup project. Locate the Setup.exe file in the bin directory to run, we can find the corresponding key value in the registry after the installation is finished. Restarting the computer system will automatically run the software we set up.

. NET registry

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.