C#.Net操作注册表RegistryKey

看看RegistryKey的帮助就知道了,这个东西不复杂,比如:

1、加键加值 

string appName = "PowerOffOnTime";
//获取执行该方法的程序集,并获取该程序集的文件路径(由该文件路径可以得到程序集所在的目录)
string thisExecutablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
//SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run注册表中这个路径是开机自启动的路径
Microsoft.Win32.RegistryKey Rkey =
    Microsoft.Win32.Registry.LocalMachine.CreateSubKey
    ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
Rkey.SetValue(appName, thisExecutablePath); 
Rkey.Close();
效果如下:

2、读得键值

Microsoft.Win32.RegistryKey Rkey =
                   Microsoft.Win32.Registry.LocalMachine.CreateSubKey
                   ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
string  Key = Rkey.GetValue("PowerOffOnTime").ToString();
效果如下:


3、在注册表中新建文件,并在文件夹下增加键值

string appName = "PowerOffOnTime";
//获取执行该方法的程序集,并获取该程序集的文件路径(由该文件路径可以得到程序集所在的目录)
string thisExecutablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
RegistryKey src = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey("Microsoft", true).OpenSubKey("Windows", true).OpenSubKey("CurrentVersion", true).OpenSubKey("Run", true);
//写入注册表项(即文件夹)
RegistryKey red = src.CreateSubKey("PowerOffOnTime");
//在这个文件夹内写入值
red.SetValue(appName, thisExecutablePath);
效果如下:


4、删键

RegistryKey src = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey("Microsoft", true).OpenSubKey("Windows", true).OpenSubKey("CurrentVersion", true).OpenSubKey("Run", true);
src.DeleteSubKey("PowerOffOnTime");
效果如下:


小注:

        修改注册表的尤其是开机自动启动的注册表最大的好处就是自己写小程序的时候,可以每天自动执行一下,比如说:你想写一个到时自动关机的小程序........



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。