积极答复者
c#调用advapi32.dll的LogonUser函数,以admin登陆后修改注册表,仍然提示没有权限

问题
-
如题,我引入advapi32.dll的LogonUser函数,在程序里以admin用户登录,在修改注册表的时候,仍然提示没有修改权限,我切到admin用户下就可以修改,admin属于本机administrators用户组,PC20140402是本机机器名,求高手帮忙看下
代码如下
IntPtr admin_token = default(IntPtr); WindowsIdentity wid_admin = null; WindowsImpersonationContext wic = null; //在程序中模拟域帐户登录 if (WinLogonHelper.LogonUser("admin", "PC20140402", "sina.com", 9, 0, ref admin_token) != 0) { using (wid_admin = new WindowsIdentity(admin_token)) { using (wic = wid_admin.Impersonate()) { RegistryKey regKey = Registry.LocalMachine; RegistryKey subKey = regKey.OpenSubKey(sPath,Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree,System.Security.AccessControl.RegistryRights.SetValue); string sValue = subKey.GetValue("Start").ToString(); subKey.SetValue("Start", "3",RegistryValueKind.DWord); this.labStatus.Text = "已开启"; } } }
在打开subkey的时候,仍然提示没有权限
答案
-
要修改注册表,必须让程序以管理员权限运行才行。让程序以管理员权限运行的方法如下:
在你的工程里面添加一个“app.manifest”文件(右键点击工程,选择添加新建项,然后选择应用程序清单文件即可)。如果你用的是VS2005,则无法直接添加清单文件,比较麻烦,但也有解决方法。
新建清单文件后,把以下内容拷贝到你的清单文件里面去,然后重新编译程序即可、
<?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"> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> <applicationRequestMinimum> <defaultAssemblyRequest permissionSetReference="Custom" /> <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" /> </applicationRequestMinimum> </security> </trustInfo> </asmv1:assembly>
如果是使用VS2005,则需要自己用记事本建一个app.manifest文件保存,然后用vs2005打开生成的exe执行文件,右键单击exe文件名选择添加资源导入app.manifest,文件类型为RT_MANIFEST,将其ID改为1,再保存打开的exe即可。
- 已建议为答案 Fred BaoModerator 2014年4月18日 1:48
- 取消建议作为答案 Dawnlight_Mr 2014年4月18日 2:15
- 已标记为答案 Dawnlight_Mr 2014年4月18日 2:58
全部回复
-
要修改注册表,必须让程序以管理员权限运行才行。让程序以管理员权限运行的方法如下:
在你的工程里面添加一个“app.manifest”文件(右键点击工程,选择添加新建项,然后选择应用程序清单文件即可)。如果你用的是VS2005,则无法直接添加清单文件,比较麻烦,但也有解决方法。
新建清单文件后,把以下内容拷贝到你的清单文件里面去,然后重新编译程序即可、
<?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"> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> <applicationRequestMinimum> <defaultAssemblyRequest permissionSetReference="Custom" /> <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" /> </applicationRequestMinimum> </security> </trustInfo> </asmv1:assembly>
如果是使用VS2005,则需要自己用记事本建一个app.manifest文件保存,然后用vs2005打开生成的exe执行文件,右键单击exe文件名选择添加资源导入app.manifest,文件类型为RT_MANIFEST,将其ID改为1,再保存打开的exe即可。
- 已建议为答案 Fred BaoModerator 2014年4月18日 1:48
- 取消建议作为答案 Dawnlight_Mr 2014年4月18日 2:15
- 已标记为答案 Dawnlight_Mr 2014年4月18日 2:58