Click here to Skip to main content
12,942,930 members (74,513 online)
Rate this:
Please Sign up or sign in to vote.
See more:

Friends I am working on a project called computer scanner, But when i run my code in order to scan my hard drives its gives me an exception "Access Denied to Documents and Setting Folders" , I am using some code which would allow me to run my code as a Administrator, but still showing me the same exception.

The codes are:
WindowsIdentity CurrentIdentity = WindowsIdentity.GetCurrent();
                WindowsPrincipal CurrentPrincipal = new      WindowsPrincipal(CurrentIdentity);
                if (CurrentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
//Scanning Code will Execute

after using this code it still giving me an exception...

Please Help.
Posted 28-Nov-12 2:21am
Updated 28-Nov-12 2:26am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You will need to add a custom manifest to your project and then include it into the project's properties. The manifest is a simple xml file in the following format:
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestversion="1.0">
  <assemblyidentity version="" name="" />
  <trustinfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <requestedprivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedexecutionlevel level="requireAdministrator" uiaccess="false" />

You can find the default manifest file for your project in the bin\Debug and bin\Release directories, so you can use one of those as the basis for your custom file. The key change is in the node, where the requestedExecutionLevel level must be set equal to requireAdministrator as shown in the sample above.
Nikhil@123 29-Nov-12 1:12am
Sir, I have tried the Followed code but still i am getting same Error..
Richard MacCutchan 29-Nov-12 5:00am
I have explained to you a number of times in your other question why you cannot access this folder. I have also shown you where the information is that explains how you can change the access control in order to gain access; having administrator rights is not enough. These folders are given the highest levels of security for a reason - to stop inadvertent damage to their contents which could make the system unusable. If you are planning to use this program anywhere other than on your own system then you are going to run into the same problem.
Nikhil@123 29-Nov-12 23:27pm
Sir, Then how other Registered Antivirus like (Kaspersky, Norton, Avast) have an access to scan those folders, and how they avail Admin Rights to their Program. Why they dont get "Access Denied" exception in order to scan those Folders..
Richard MacCutchan 30-Nov-12 3:36am
I am not sure how they do it, but probably have some co-operating service running under the system account.
Nikhil@123 30-Nov-12 23:21pm
Sir, So What should i do now?
Richard MacCutchan 1-Dec-12 5:51am
I don't know; try some more research perhaps. See if you can get any information from some of the existing anti-virus makers. Try some of the technical blogs on Microsoft's website.
Nikhil@123 1-Dec-12 8:34am
Thank you Sir for talking to me. :) will keep searching until and unless i dont get any clue regarding making an Antivirus//
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It sounds like you are running into an issue where either the user does not have admin rights or the UAC dialog is not being triggered so the application does not run with admin rights. If your issue is the first one (no admin rights), you will need to work with impersonation:[^]

If the problem is the second one (UAC), you will need to kick off the UAC prompt before you fire your code:[^]

Neither of these options are really that great, but they will work. However, the better option would be to run your application as a service. That way you can choose what level of access rights the user has who is running it and you won't be stopped by the UAC. Don't forget that you can also run the application as a different user (or administrator) by right-clicking on it (or shift-right-click) and specifying the user.
Nikhil@123 29-Nov-12 7:46am
still not working. :(
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Just Change the Administrator Rights :)

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,633
CHill60 2,880
Maciej Los 2,268
Jochen Arndt 1,900
ppolymorphe 1,755

Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100