Solved

Exception while reading the registry key

Posted on 2010-09-18
11
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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