2016-01-26 52 views
0

我有一個工具應用程序在WPF(.NET 4.0),需要訪問註冊表並更改子項值。但是當我嘗試在Windows Server 2008 x64中執行x86中構建的應用程序時,出現錯誤「SecurityException請求的註冊表訪問不被允許」。當我在Windows 8 x64中執行相同的應用程序時,該應用程序完美工作。SecurityException不允許請求的註冊表訪問

我試圖給予註冊表項的權限,甚至改變了所有者,但它並不好。

該應用程序正在運行以管理員和我這個值設置爲清單文件:

<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> 

這是改變了值的方法:

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(); 
} 

當我改變構建到AnyCPU,應用程序在WinServer 2008 x64上按預期更改值,但在構建爲x86時不會。在我的Windows 8 x64中,它完全可以在x86和x64上運行。 你們有什麼線索嗎?

回答

2

因此,當試圖從運行在Windows Server 2008上的32位應用程序訪問顯式指定的64位註冊表視圖位置時發生異常。這是因爲在Windows Server 2008上,註冊表是reflected導致64位32位應用程序無法訪問值的副本。在更高版本的Windows上,例如在Win8 x64上,註冊表值是共享的,因此任何一個視圖都可以被32位應用程序訪問。

儘管您沒有解釋爲什麼應用程序需要寫入Windows Server 2008上的兩個物理位置,但我不認爲這會對您造成問題,因爲您可以使用AnyCPU目標構建來同時更新32位和64位的位置,因爲你的代碼依次明確地打開每個視圖,所以我不認爲你需要一個32位版本,所以這個問題只是一個好奇心嗎?

Issue reading x64 registry keys的回答也可能對此有所幫助。

+0

我們有一個應用程序使用SqlServer,Oracle和Informix作爲可能的數據庫。由於informix,我們的應用程序必須構建爲x86才能在環境x86上正常工作。 此工具也必須在x86 Windows中工作,而且我不希望每次都生成兩個構建版本。我想知道我是否可以將該工具保留爲x86,並且適用於這兩種平臺。 – Rafael

相關問題