(1) Write
1. Create a file
Create a Registry File: *. Reg with the following content:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE \ SOFTWARE \ test]
"Server" = "192.168.66.22"
"Database" = "northwind"
"User" = "Xiaowang"
"Password" = "123456"
Where:
I.
[HKEY_LOCAL_MACHINE \ SOFTWARE \ test]: indicates the path. If the path does not exist, the system automatically creates the path.
II.
"Server" = "192.168.66.22"
"Database" = "northwind"
"User" = "Xiaowang"
"Password" = "123456"
Keys and values. Keys on the left and keys on the right. Read data by key during reading.
2. Double-click to run. It will automatically put the key value under the configured path.
(2) reading
Open the namespace: using Microsoft. Win32;
1. Method
// Parameter 1 indicates the path. For example, HKEY_LOCAL_MACHINE \ Software
// Parameter 2 indicates the key. Custom
Public static object getregvalue (string strregpath, string strname)
{
Strregpath = strregpath. Trim ();
// Recipient
Object objret;
// If the name is null, an exception is thrown.
If (strname = "")
{
Throw new argumentnullexception (strname, "the key value cannot be blank! ");
}
// Remove the "\" Character
If (strregpath. startswith ("\\"))
{
Strregpath = strregpath. substring (1, strregpath. Length-1 );
}
If (strregpath. endswith ("\\"))
{
Strregpath = strregpath. substring (0, strregpath. Length-1 );
}
// Split the root key and Path
String strrootkey, strpath;
Int intindex = strregpath. indexof ("\\");
Strrootkey = strregpath. substring (0, intloc). toupper ();
strpath = strregpath. substring (intindex + 1, strregpath. length-intindex-1);
registrykey _ root;
switch (strrootkey)
{< br> case "hkey_classes_root ":
_ root = registry. classesroot;
break;
case "hkey_current_config":
_ root = registry. currentconfig;
break;
case "HKEY_CURRENT_USER":
_ root = registry. currentuser;
break;
case "hkey_dyn_data":
_ Root = Registry. dyndata;
break;
case "HKEY_LOCAL_MACHINE":
_ root = registry. localmachine;
break;
case "hkey_performance_data":
_ root = registry. performancedata;
break;
case "HKEY_USERS":
_ root = registry. users;
break;
default:
throw new exception ("path not found! ");
}
Try
{
// Open the Registry path key
Registrykey regkey = _ root. opensubkey (@ strpath );
// Value
Objret = regkey. getvalue (strname );
}
Catch (exception E)
{
Throw E;
}
Return objret;
}
2. Usage:
String strconnectstring = getregvalue (@ "HKEY_LOCAL_MACHINE \ SOFTWARE \ test", "strconnstring"). tostring ();