即使以管理员身份运行,也无法写入win7注册表项 [英] Cannot write to the win7 registry key even when run as admin

查看:110
本文介绍了即使以管理员身份运行,也无法写入win7注册表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

当我尝试在SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run \中创建子项时,出现"UnauthorizedAccessException"异常.

Hi All,

"UnauthorizedAccessException" exception appear when i try to create sub key in SOFTWARE\Microsoft\Windows\CurrentVersion\Run\.

try
            {
                string TypeLib = "TypeLib";
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\");
                RegistryPermission perm = new RegistryPermission(RegistryPermissionAccess.AllAccess, key.Name);
                perm.AddPathList(RegistryPermissionAccess.Create, key.Name + "\\TypeLibJD");
                string[] subKeys = key.GetSubKeyNames();
                if (Array.IndexOf(subKeys, TypeLib) >= -1)
                {
                    RegistryKey typeLibKey = key.CreateSubKey("//TypeLib" );
                    typeLibKey.SetValue("RunOnStartup", System.Reflection.Assembly.GetExecutingAssembly().Location);
                }
            }
            catch(UnauthorizedAccessException ex)
            {
                MessageBox.Show("Cannot Make This Application Run as startup");
            }



我通过在app.manifest中编写此应用程序来使该应用程序以管理员身份运行



and i make this app run as administrator by writing this in app.manifest

<pre lang="xml"><trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the
            requestedExecutionLevel node with one of the following.

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

            Specifying requestedExecutionLevel node will disable file and registry virtualization.
            If you want to utilize File and Registry Virtualization for backward
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>




但是UnauthorizedAccessException仍然出现

请帮忙.

在此先感谢.




but the UnauthorizedAccessException still appear

Any help please.

Thanks in Advance.

推荐答案

假定您要写入HKEY_LOCAL_MACHINE,则需要以提升的特权运行.检查构建属性的清单部分中可用的选项.
Assuming you are trying to write into HKEY_LOCAL_MACHINE then you need to run with elevated privileges. Check the options available in the manifest section of your build properties.


当然,您需要以提升的特权运行此类应用程序.即使您已经以管理员身份登录,在Windows 7中,您也需要以管理员身份运行应用程序.请参阅资源管理器上下文菜单.

—SA
Sure, you need to run such application with elevated privileges. Even if you already logged as administrator, in Windows 7 you also need to run your application as administrator. See your Explorer context menu.

—SA


以管理员身份运行应用程序或在项目中使用app.manifest

在清单文件中更改此代码:

run your application as administrator or use app.manifest in your project

change this code in manifest file :

<security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the
            requestedExecutionLevel node with one of the following.

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

            Specifying requestedExecutionLevel node will disable file and registry virtualization.
            If you want to utilize File and Registry Virtualization for backward
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

      </requestedPrivileges>
    </security>


这篇关于即使以管理员身份运行,也无法写入win7注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆