Solved

Exception while reading the registry key

Posted on 2010-09-18
11
399 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

13 Experts available now in Live!

Get 1:1 Help Now