Home  >  Article  >  Backend Development  >  C#.Net operation registryRegistryKey

C#.Net operation registryRegistryKey

黄舟
黄舟Original
2017-02-25 11:06:361771browse

看看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");

效果如下:



小注:

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

 以上就是C#.Net操作注册表RegistryKey的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn