Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
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
Edited 28-Nov-12 2:26am
v2
Rate this: bad
good
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="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.
  Permalink  
Comments
Nikhil@123 at 29-Nov-12 1:12am
   
Sir, I have tried the Followed code but still i am getting same Error..
Richard MacCutchan at 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 at 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 at 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 at 30-Nov-12 23:21pm
   
Sir, So What should i do now?
Richard MacCutchan at 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 at 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
good
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:
 
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.
  Permalink  
Comments
Nikhil@123 at 29-Nov-12 7:46am
   
still not working. :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Just Change the Administrator Rights Smile | :)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 278
1 Sergey Alexandrovich Kryukov 236
2 PhilLenoir 179
3 ChauhanAjay 166
4 ClimerChinna 150
0 Sergey Alexandrovich Kryukov 6,140
1 OriginalGriff 5,195
2 CPallini 2,473
3 Richard MacCutchan 1,607
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
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