If you look at the code through Reflector, you will see that if returning from the Win32Native.RegCreateKeyEx() native method returns anything other than error code 0, or if the result of the operation is null , it will then return null to you.
The possible error here is that the wrong permissions are used, as a result of which the refusal code is returned.
The code for this method is as follows:
[ComVisible(false)] public unsafe RegistryKey CreateSubKey(string subkey, RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity) { ValidateKeyName(subkey); ValidateKeyMode(permissionCheck); this.EnsureWriteable(); subkey = FixupName(subkey); if (!this.remoteKey) { RegistryKey key = this.InternalOpenSubKey(subkey, permissionCheck != RegistryKeyPermissionCheck.ReadSubTree); if (key != null) { this.CheckSubKeyWritePermission(subkey); this.CheckSubTreePermission(subkey, permissionCheck); key.checkMode = permissionCheck; return key; } } this.CheckSubKeyCreatePermission(subkey); Win32Native.SECURITY_ATTRIBUTES structure = null; if (registrySecurity != null) { structure = new Win32Native.SECURITY_ATTRIBUTES(); structure.nLength = Marshal.SizeOf(structure); byte[] securityDescriptorBinaryForm = registrySecurity.GetSecurityDescriptorBinaryForm(); byte* pDest = stackalloc byte[1 * securityDescriptorBinaryForm.Length]; Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length); structure.pSecurityDescriptor = pDest; } int lpdwDisposition = 0; SafeRegistryHandle hkResult = null; int errorCode = Win32Native.RegCreateKeyEx(this.hkey, subkey, 0, null, 0, GetRegistryKeyAccess(permissionCheck != RegistryKeyPermissionCheck.ReadSubTree), structure, out hkResult, out lpdwDisposition); if ((errorCode == 0) && !hkResult.IsInvalid) { RegistryKey key2 = new RegistryKey(hkResult, permissionCheck != RegistryKeyPermissionCheck.ReadSubTree, false, this.remoteKey, false); this.CheckSubTreePermission(subkey, permissionCheck); key2.checkMode = permissionCheck; if (subkey.Length == 0) { key2.keyName = this.keyName; return key2; } key2.keyName = this.keyName + @"\" + subkey; return key2; } if (errorCode != 0) { this.Win32Error(errorCode, this.keyName + @"\" + subkey); } return null; }
Kyle rozendo
source share