Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不允许SecurityException请求的注册表访问_C#_Wpf_Windows_Permissions_Registry - Fatal编程技术网

C# 不允许SecurityException请求的注册表访问

C# 不允许SecurityException请求的注册表访问,c#,wpf,windows,permissions,registry,C#,Wpf,Windows,Permissions,Registry,我在WPF(.NET 4.0)中有一个工具应用程序,它需要访问注册表并更改子项值。但是,当我尝试在Windows Server 2008 x64中执行x86内置的应用程序时,会出现错误“SecurityException请求的注册表访问不允许”。当我在Windows 8 x64中执行相同的应用程序时,该应用程序工作得非常好 我试图为注册表项授予权限,甚至更改所有者,但这没有用 应用程序正在以管理员身份运行,我将此值设置为清单文件: <trustInfo xmlns="urn:schemas

我在WPF(.NET 4.0)中有一个工具应用程序,它需要访问注册表并更改子项值。但是,当我尝试在Windows Server 2008 x64中执行x86内置的应用程序时,会出现错误“SecurityException请求的注册表访问不允许”。当我在Windows 8 x64中执行相同的应用程序时,该应用程序工作得非常好

我试图为注册表项授予权限,甚至更改所有者,但这没有用

应用程序正在以管理员身份运行,我将此值设置为清单文件:

<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" ID="Custom" SameSite="site" Unrestricted="true" />
  </applicationRequestMinimum>
</security>
当我将构建更改为AnyCPU时,应用程序会按照WinServer 2008 x64上的预期更改值,但在构建为x86时不会更改。在我的Windows 8 x64中,它在x86和x64中都能完美工作。
你们有什么线索吗?

所以当试图从Windows Server 2008上运行的32位应用程序访问显式指定的64位注册表视图位置时,会发生异常。这是因为在Windows Server 2008上,注册表导致32位应用程序无法访问值的64位副本。在更高版本的Windows上,例如在Win8 x64上,注册表值是共享的,因此32位应用程序可以访问任一视图

虽然您没有解释为什么应用程序需要写入Windows Server 2008上的两个物理位置,但我认为这不会对您造成问题,因为您可以使用以AnyCPU为目标的构建来更新32位和64位位置,因为您的代码会显式地依次打开每个视图,所以我认为你不需要32位的构建,所以这个问题只是好奇,对吧

答案
这可能也会有所帮助。

因此,当尝试从Windows Server 2008上运行的32位应用程序访问显式指定的64位注册表视图位置时,会发生异常。这是因为在Windows Server 2008上,注册表导致32位应用程序无法访问值的64位副本。在更高版本的Windows上,例如在Win8 x64上,注册表值是共享的,因此32位应用程序可以访问任一视图

虽然您没有解释为什么应用程序需要写入Windows Server 2008上的两个物理位置,但我认为这不会对您造成问题,因为您可以使用以AnyCPU为目标的构建来更新32位和64位位置,因为您的代码会显式地依次打开每个视图,所以我认为你不需要32位的构建,所以这个问题只是好奇,对吧

答案
这可能也有点帮助。

我们有一个应用程序,它使用SqlServer、Oracle和Informix作为可能的数据库。由于informix,我们的应用程序必须构建为x86,才能在x86环境中正常工作。这个工具也必须在x86 Windows中工作,我不想每次都生成两个版本。我想知道我是否可以将该工具保持为x86,并且它可以在两种平台上工作。我们有一个应用程序,它使用SqlServer、Oracle和Informix作为可能的数据库。由于informix,我们的应用程序必须构建为x86,才能在x86环境中正常工作。这个工具也必须在x86 Windows中工作,我不想每次都生成两个版本。我想知道是否可以将该工具保持为x86,并且它可以在两种平台上工作。
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey(RegistryHelper.Path64, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);

if (localKey != null)
{
    localKey.SetValue(RegistryHelper.CsKey, CryptographyHelper.Encrypt(CryptographyHelper.DefaultKey, cs.ConnectionString));
    localKey.SetValue(RegistryHelper.ProviderKey, provider);
    localKey.Close();
}

localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
localKey = localKey.OpenSubKey(RegistryHelper.Path32, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);

if (localKey != null)
{
    localKey.SetValue(RegistryHelper.CsKey, CryptographyHelper.Encrypt(CryptographyHelper.DefaultKey, cs.ConnectionString));
    localKey.SetValue(RegistryHelper.ProviderKey, provider);
    localKey.Close();
}