Click here to Skip to main content
15,883,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
            }


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>



but the UnauthorizedAccessException still appear

Any help please.

Thanks in Advance.
Posted
Updated 13-Jun-11 1:16am
v4

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.
 
Share this answer
 
Comments
Wonde Tadesse 12-Jun-11 17:06pm    
Correct. 5+
Kim Togo 13-Jun-11 2:31am    
Correct my 5. Some many parts of the registry requires elevated privileges.
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
 
Share this answer
 
Comments
Wonde Tadesse 12-Jun-11 17:07pm    
Correct.5+
Sergey Alexandrovich Kryukov 12-Jun-11 23:53pm    
Thank you, Wonde.
--SA
Kim Togo 13-Jun-11 2:30am    
My 5. Good answer.
Sergey Alexandrovich Kryukov 13-Jun-11 2:39am    
Thank you, Kim.
--SA
run your application as administrator or use app.manifest in your project

change this code in manifest file :

XML
<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>
 
Share this answer
 
Comments
Kim Togo 13-Jun-11 2:29am    
Correct. My 5.
Please see http://stackoverflow.com/questions/4463706/net-windows-service-cannot-write-to-registry-key-unauthorizedaccessexception[^]

It's written there that RegistryKey.OpenSubKey(string) does not open the key for writing. Try using the OpenSubKey(string, bool) overload to specify that you want the key to be writable.

Please also note that administrator permissions are required if you are writing to the Registry.LocalMachine; on the contrary, writing to the Registry.CurrentUser doesn't require administrator permissions (current user has write permissions to its own registry key by default).
 
Share this answer
 
v2
Comments
stankovski 20-Apr-12 14:44pm    
Perfect! That solved it for me.
Member 10131044 31-Jul-17 2:07am    
Thanks! I struggled to get it working and your solution works like a charm!
Hope this[^]might help you to solve your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900