c# win7注册表【Registry】 操作
来源:互联网 发布:苏州科技城软件大厦 编辑:程序博客网 时间:2024/04/28 16:55
所以,在操作win7注册表是必须要有管理员身份才能操作注册表信息,这样不会被UAC(用户帐户控制)阻止访问操作注册表信息。
这里讲一 C# 小列子(高手请绕过此地!), 我们平时都是在xp开发比较多...不过现在很多开发人员也在win7下开发了。
下面是在 LocalMachine下的 一下注册表操作 ,就不详说了privatestatic void DeleteRegistry(string name)
{
string[] aimnames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("Microsoftss",true);
aimnames = aimdir.GetSubKeyNames();
foreach (string aimKeyin aimnames)
{
if (aimKey == name)
aimdir.DeleteSubKeyTree(name);
}
}
private static string GetData(string name)
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("Microsoftss",true);
registData = aimdir.GetValue(name).ToString();
return registData;
}
private static void WriteValue(string name,string tovalue)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.CreateSubKey("Microsoftss");
aimdir.SetValue(name, tovalue);
}
private static bool IsExit(string name)
{
bool _exit = false;
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("Microsoftss",true);
subkeyNames = aimdir.GetValueNames();
foreach (string keyNamein subkeyNames)
{
if (keyName == name)
{
_exit =true;
return _exit;
}
}
return _exit;
}
比如,当我们在xp系统下 软件执行Writevalue("版本号","v1.0.0.1"),一切都ok! 然后乐的就抛给了客户...没有思考。
一天客户换了win7系统 ,运了软件了。客户拉着42号鞋子的脸说,怎么软件在win7不能运行了....。问题就是上面开头说的了。
解决方法是有地....
这里列举几种.
第一种、教教客户、或者技术服务人员就好了。这个方法不需要修改我们的代码这是好处之一。只要, 点击.exe 右键
,在弹出的菜单中选择【属性】, 选择【兼容性】项,并勾选【以管理员身份运行此程序】就ok了。简单吧
第二种、也算简单,不过我们就要重新生成软件了。首先、在程序中加入MANIFEST资源,右击工程在菜单->【属性】->【安全性】,在界面中勾选【启用ClickOnce安全设置】,在项目的Properties下就有自动生成app.manifest文件。
文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC 清单选项
如果希望更改 Windows 用户帐户控制级别,请用以下节点之一替换
requestedExecutionLevel 节点。
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
<requestedExecutionLevel level="highestAvailable" uiAccess="false"/>
如果您希望利用文件和注册表虚拟化提供
向后兼容性,请删除 requestedExecutionLevel 节点。
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom"/>
<PermissionSetclass="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site"/>
</applicationRequestMinimum>
</security>
</trustInfo>
</asmv1:assembly>
把 <requestedExecutionLevel level="asInvoker" uiAccess="false"/> 的 asInvoker 改为"requireAdministrator"。重新编译一下就ok了。
看到这个了吧!
这就是UAC(用户帐户控制)了
<!-- UAC 清单选项 如果希望更改 Windows 用户帐户控制级别,请用以下节点之一替换 requestedExecutionLevel 节点。 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> 如果您希望利用文件和注册表虚拟化提供 向后兼容性,请删除 requestedExecutionLevel 节点。 -->
还有其他的方法。不过上面两种比较简单...。
- c# win7注册表【Registry】 操作
- Operating the registry help class(C#读写注册表操作)
- C# Win7、Vista系统注册表操作
- 使用Registry和RegistryKey操作注册表
- 使用Registry和RegistryKey操作注册表
- 使用Registry和RegistryKey操作注册表
- win7 操作注册表
- C#简单的注册表操作类(兼容Win7)
- 关于win7一些注册表操作
- C#操作注册表
- C#操作注册表
- C#中操作注册表
- C#操作注册表
- c#中操作注册表
- C#操作注册表
- C#操作注册表
- C#操作注册表
- c#操作注册表
- 内核中的 likely() 与 unlikely()
- Command Line Shell For SQLite (页面调整中)
- overring virtual functions
- std::string
- linux下解压命令大全
- c# win7注册表【Registry】 操作
- C# 中的委托和事件(给力你懂得!)
- 校车管理条例即将出台 需专业厂商采购
- msxml3.dll 错误 '80072efd' A connection with the server could not be established
- 电脑经常自动关机解决方案二
- 大学毕业后要知道的
- javaScript闭包
- PHP header跳转
- 含有clob字段的dmp文件,不同表空间导入导出问题.