Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exception while reading the registry key

Posted on 2010-09-18
11
Medium Priority
?
411 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 884 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 892 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 892 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 892 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 884 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 884 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 884 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 892 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 224 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

886 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