C # Operations Registry WindowsRegistry

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;using Microsoft.Win32;namespace bugsbox.application.core{public class WindowsRegistry {const string registry_item_path = "Software\\pharmac        Yinst "; <summary>//Create registry key///</summary>//<param name= "Registryitempath" ></par am> public static void Createregistry (String registryitempath = Registry_item_path) {REGISTRY            Key key = Registry.localmachine; RegistryKey software = key.            CreateSubKey (Registryitempath); Create a new registry key named Test under HKEY_LOCAL_MACHINE\Software.        It does not affect if it already exists!        }//<summary>//Get the registry value///</summary>//<returns></returns> public static string GetRegistryValue (string key, String registryitempath = Registry_item_path) {str            ing info = "";            RegistryKey Key; Key = Registry.localmachine;           var Myreg = Key.opensubkey (Registryitempath); info = Myreg. GetValue (Key).            ToString (); Myreg.            Close ();        return info; }///<summary>//Modify registry key values///</summary>//<param Name= "subkey" ></par        am>//<param name= "val" ></param>///<param name= "Registryitempath" ></param>             public static void SetRegistryValue (String subkey, String val, string registryitempath = Registry_item_path) { using (RegistryKey key = Registry.localmachine) {RegistryKey software = key. OpenSubKey (Registryitempath, true); The item must already exist software.                SetValue (subkey, Val); Under HKEY_LOCAL_MACHINE\Software\Test, create a key value named "Test" with a value of "blog Park".                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 (); }}///<summary>//Delete registry key///</summary> public static void Deleteregist            Ry (String registryitempath = Registry_item_path) {RegistryKey key = Registry.localmachine; Key. DeleteSubKey (Registryitempath, true); The method has no return value and can be called directly by key.        Close (); }///<summary>//Delete registry value///</summary> public static void Deleteregistryvalue (s Tring subkey, String registryitempath = Registry_item_path) {RegistryKey Delkey = Registry.localmachine .            OpenSubKey (Registryitempath, true);            Delkey.deletevalue (subkey);        Delkey.close ();        }//<summary>//Determine if the registry key exists///</summary>//<returns></returns>            public static bool Isregedititemexist () {string[] subkeynames; RegistryKey HKML = Registry.localmachIne RegistryKey software = hkml.            OpenSubKey ("Software"); RegistryKey software = hkml.              OpenSubKey ("Software", true); Subkeynames = software.            Getsubkeynames ();                Gets the sequence of the names of all subkeys under the key and passes them to the predetermined array in the foreach (String keyName in Subkeynames)//traversal entire array { if (KeyName = = "Test")//determine the name of the subkey {HKML.                    Close ();                return true; }} hkml.            Close ();        return false;        }//<summary>//Determine if key value exists///</summary>//<returns></returns>            public static bool Isregeditkeyexist (String subkey, String Registryitempath = Registry_item_path) {            String[] Subkeynames;            RegistryKey hkml = registry.localmachine; RegistryKey software = hkml.            OpenSubKey (Registryitempath); RegistryKey software = hkml. OpenSubKey ("SOFTware\\test ", true); Subkeynames = software.            GetValueNames ();  Gets the sequence of names of all the key values under that key, and passes to the predetermined array of foreach (String keyName in Subkeynames) {if (KeyName = = subkey)//Determine the name of the key value {hkml.                    Close ();                return true; }} hkml.            Close ();        return false; }    }}

C # Operations Registry WindowsRegistry

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.