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

C#.Net operation registryRegistryKey

黄舟
Release: 2017-02-25 11:06:36
Original
1965 people have browsed it

看看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();
Copy after login

效果如下:


2、读得键值


Microsoft.Win32.RegistryKey Rkey =
                   Microsoft.Win32.Registry.LocalMachine.CreateSubKey
                   ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
string  Key = Rkey.GetValue("PowerOffOnTime").ToString();
Copy after login

效果如下:



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);
Copy after login

效果如下:



4、删键


RegistryKey src = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey("Microsoft", true)
.OpenSubKey("Windows", true).OpenSubKey("CurrentVersion", true).OpenSubKey("Run", true);
src.DeleteSubKey("PowerOffOnTime");
Copy after login

效果如下:



小注:

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

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template