Solved

Exception while reading the registry key

Posted on 2010-09-18
11
400 Views
Last Modified: 2012-05-10
Below code is giving error as "Requested registry access is not allowed."


reg = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{03CBA9E5-BAC9-48B4-834B-3BBA552236E7}", True)
            Dim strKey As String
            strKey = reg.GetValue("AddressType")


Please provide the solution

Thanks
0
Comment
Question by:futurebuilders
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 221 total points
ID: 33707437
you need to turn off UAC or to allow your program to access the registry. Windows7 denies access to registry keys that were previously available under XP.
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 223 total points
ID: 33707523
If you only want to READ the value remove the (TRUE) part from your call and change it to FALSE.

reg = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{03CBA9E5-BAC9-48B4-834B-3BBA552236E7}", False)

Open in new window

0
 

Author Comment

by:futurebuilders
ID: 33707856
Thanks nepaluz,

How can i turn off UAC. I am unable to see any option in project file. I am using VS 2005.

Please provide the solution


Thanks
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 223 total points
ID: 33707875
You can READ without permissions in user mode but you can't WRITE to HKEY_LOCAL_MACHINE in user mode.
You must have just ignored the comment entirely because if you tried it would notice it works but if you also need to write then it's not going work unless you elevate the process. Turning off UAC is your personal decision but it's not how you work around permission issues.
0
 

Author Comment

by:futurebuilders
ID: 33707884
Yes eql1044,

When I make it false I am able to read.But my requirement is after reading the key, If it deos not exists I have to write. So What to do to write in to HKEy Local machine.

PLease provide the solution

Thanks In advance
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 223 total points
ID: 33707946
You have to manifest your application to request the administrator token because thats the only way you will get your process the appropriate permissions to write into that part of the registry.
Create and Embed an Application Manifest (UAC)
http://msdn.microsoft.com/en-us/library/bb756929.aspx 
Create a new text file that is the same as your executable name but with .manifest tag on the end: example: (myexename.exe.manifest)
Add the code XML in the code block to the file and save then drop the file in the same directory as your executable.
 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 

  <assemblyIdentity version="1.0.0.0"

     processorArchitecture="X86"

     name="YourApplicationName"

     type="win32"/> 

  <description>Description of your application</description> 

  <!-- Identify the application security requirements. -->

  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">

    <security>

      <requestedPrivileges>

        <requestedExecutionLevel

          level="requireAdministrator"

          uiAccess="false"/>

        </requestedPrivileges>

       </security>

  </trustInfo>

</assembly>

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 221 total points
ID: 33708243
would this be similar to setting the requested execution level via the application properties' "View Windows Settings" as per below?

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 221 total points
ID: 33708245
sorry, here's the code
<?xml version="1.0" encoding="utf-8"?>

<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />

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



            If you want to utilize File and Registry Virtualization for backward 

            compatibility then delete the requestedExecutionLevel node.

        -->

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

      </requestedPrivileges>

      <applicationRequestMinimum>

        <defaultAssemblyRequest permissionSetReference="Custom" />

        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />

      </applicationRequestMinimum>

    </security>

  </trustInfo>

</asmv1:assembly>

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 221 total points
ID: 33708255
when you change theexecution level this way though, you can NOT have your application as a clickonce. What are the drawbacks to this?
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 223 total points
ID: 33708286
Hi nepaluz, It is the same but I think he is using VS2005 and that setting in the IDE was only introduced in VS2008 or later :(
0
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 56 total points
ID: 33710675
In the solution explorer, double click on My Project -> click on "View UAC Settings" button, change the xml code to the following code:
<?xml version="1.0" encoding="utf-8"?>

<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>

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



            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>

</asmv1:assembly>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now