(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)
{
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 ();