Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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:
C#
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
Updated 28-Nov-12 2:26am
v2

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:
XML
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestversion="1.0">
  <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">
        <requestedexecutionlevel level="requireAdministrator" uiaccess="false" />
      </requestedprivileges>
    </security>
  </trustinfo>
</assembly>

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.
 
Share this answer
 
Comments
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..
Thanks
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?
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:

http://stackoverflow.com/questions/1168571/run-code-as-a-different-user-c[^]

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

http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7[^]

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.
 
Share this answer
 
Comments
Nikhil@123 29-Nov-12 7:46am    
still not working. :(
Just Change the Administrator Rights :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900