引入:using Microsoft.Win32;

(1)读取注册表信息

代码:

RegistryKey rsg = null;                   
 rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\isBIM\\isBIMQS2017\\Revit2016", false);
string PackageLocation = rsg.GetValue("InstallationLocation").ToString();

第二行的 true/false表示是否可修改

(2)写入注册表信息

代码:

string isBIMProductsInfo = String.Format("SOFTWARE\\YourPath\\{0}\\{1}", path1,path2);
RegistryKey isBIMProductRsg = Registry.LocalMachine.OpenSubKey(isBIMProductsInfo, RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl);
isBIMProductRsg.SetValue("YourProperty", YourNewValue);

注意:你可能会被提示没有权限去操作注册表,特别是LocalMachine下的注册表信息,所以你需要:
1)在项目中右键添加应用清单文件app.manifest

2) 修改requestedExecutionLevel:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

这样当程序运行到修改注册表的值时会弹出提示框,提示是否能修改注册表,即请求管理员"requireAdministrator"。

posted on 2016-07-24 14:13  airforce094  阅读(591)  评论(0编辑  收藏  举报