C # Reading, writing, deleting, and searching the registry
Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system.web.ui.html controls;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using Microsoft. Win32;
Public partial class register: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Response. Write ("the information read here" + "<br/> ");
Readregedit ();
Response. Write ("<br/> ");
Response. Write ("the information swort/swort-test" + "<br/> ");
Writeregedit ();
Response. write ("Write ended" + "<br/>"); V]; eg. 4 (Uy * z = [the source of this resource is the network programming of your learning network. net tutorial http://Www.gzU521.com] V]; eg. 4 (Uy * z =
Response. Write ("check whether there is" + "<br/> ");
Existsregedit ();
Response. Write ("viewing ends" + "<br/> ");
Response. Write ("delete" + "<br/> ");
Deleteregedit ();
Response. Write ("delete ended" + "<br/> ");
Response. Write ("check whether there is" + "<br/> ");
Existsregedit ();
Response. Write ("viewing ends" + "<br/> ");
}
/// <Summary>
/// Read the Registry
/// </Summary>
/// <Returns> </returns>
Public void readregedit ()
{
Registrykey rk = registry. currentuser;
Registrykey software = rk. opensubkey ("software ");
Registrykey Microsoft = software. opensubkey ("Microsoft ");
Registrykey windows = Microsoft. opensubkey ("Windows ");
Registrykey current = windows. opensubkey ("CurrentVersion ");
Registrykey explorer = current. opensubkey ("Explorer ");
Registrykey shell = explorer. opensubkey (@ "Shell Folders ");
Foreach (string B in shell. getvaluenames () // here shell. getvaluenames () is used instead of shell. getsubkeynames ()
{
Response. Write (B + "" + shell. getvalue (B). tostring ());
Response. Write ("<br/> ");
}
}
/// <Summary>
/// Write to the Registry
/// </Summary>
/// <Returns> </returns>
Public bool writeregedit ()
{
Try
{
Registrykey rk = registry. currentuser;
Registrykey software = rk. opensubkey ("software ");
Registrykey Microsoft = software. opensubkey ("Microsoft ");
Registrykey windows = Microsoft. opensubkey ("Windows ");
Registrykey current = windows. opensubkey ("CurrentVersion ");
Registrykey explorer = current. opensubkey ("Explorer ");
Registrykey shell = explorer. opensubkey (@ "Shell Folders", true); // you must add true to obtain the write permission.
Registrykey key = shell. createsubkey ("swort"); // create the swort directory
Key. setvalue ("swort", "test ");
// Create and write swort test in the swort directory
Response. Write ("Write successful !!!!!!!!! ");
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Delete the Registry
/// </Summary>
/// <Returns> </returns>
Public bool deleteregedit ()
{
Try
{
Registrykey rk = registry. currentuser;
Registrykey software = rk. opensubkey ("software ");
Registrykey Microsoft = software. opensubkey ("Microsoft ");
Registrykey windows = Microsoft. opensubkey ("Windows ");
Registrykey current = windows. opensubkey ("CurrentVersion ");
Registrykey explorer = current. opensubkey ("Explorer ");
Registrykey shell = explorer. opensubkey (@ "Shell Folders", true );
Registrykey swort = shell. opensubkey ("swort", true); // you must add true to obtain the write permission.
Swort. deletevalue ("swort"); // Delete the swort value. The key value is deleted and the other one is deleted.
Shell. deletesubkey ("swort", false); // you must have the permission to delete the swort directory.
Response. Write ("deleted successfully !!!!!!!!! ");
Return true;
}
Catch
{
Return false;
}
Return true;
}
/// <Summary>
/// Query whether a key value exists
/// </Summary>
/// <Returns> </returns>
Public bool existsregedit ()
{
Registrykey rk = registry. currentuser;
Registrykey software = rk. opensubkey ("software ");
Registrykey Microsoft = software. opensubkey ("Microsoft ");
Registrykey windows = Microsoft. opensubkey ("Windows ");
Registrykey current = windows. opensubkey ("CurrentVersion ");
Registrykey explorer = current. opensubkey ("Explorer ");
Registrykey shell = explorer. opensubkey (@ "Shell Folders ");
If (shell. subkeycount! = 0)
{
Registrykey swort = shell. opensubkey ("swort ");
Foreach (string B in swort. getvaluenames ())
{
If (B = "swort ")
{
Response. Write ("this key exists! ");
Return true;
}
}
Response. Write ("this key does not exist! ");
Return false;
}
Else
{Response. Write ("this key does not exist! ");
Return false;
}
}
}
(Www.gz u521.com), original address: http://www.gzu521.com/campus/article/program/200809/176006.htm