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.HtmlControls;
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("這里是讀取到的信息"+"<br/>");
ReadRegedit();
Response.Write("<br/>");
Response.Write("這里將要寫入信息swort/swort-test" + "<br/>");
WriteRegedit();
Response.Write("寫入結(jié)束" + "<br/>");
Response.Write("查看存在與否" + "<br/>");
ExistsRegedit();
Response.Write("查看結(jié)束" + "<br/>");
Response.Write("刪除" + "<br/>");
DeleteRegedit();
Response.Write("刪除結(jié)束" + "<br/>");
Response.Write("查看存在與否" + "<br/>");
ExistsRegedit();
Response.Write("查看結(jié)束" + "<br/>");
}
/// <summary>
/// 注冊(cè)表的讀取
/// </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())//這里用shell.GetValueNames()不是shell.GetSubKeyNames()
{
Response.Write( b+" "+ shell.GetValue(b).ToString());
Response.Write("<br/>");
}
}
/// <summary>
/// 注冊(cè)表的寫入
/// </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);//這里必須加true就是得到寫入權(quán)限
RegistryKey key = shell.CreateSubKey("swort");//創(chuàng)建swort目錄
key.SetValue("swort", "test");
//在swort目錄下建立寫入swort test
Response.Write("寫入成功!!!!!!!!!");
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 注冊(cè)表的刪除
/// </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);//這里必須加true就是得到寫入權(quán)限
swort.DeleteValue("swort");//刪除swort的值 這個(gè)連鍵值一起刪除了 剩下一個(gè)
shell.DeleteSubKey("swort",false);//刪除swort這個(gè)目錄 要?jiǎng)h除這個(gè)目錄 必須具有權(quán)限
Response.Write("刪除成功!!!!!!!!!");
return true;
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 查詢某個(gè)鍵值是否存在
/// </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("存在這個(gè)鍵!");
return true;
}
}
Response.Write("不存在這個(gè)鍵!");
return false;
}
else
{ Response.Write("不存在這個(gè)鍵!");
return false;
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注