3

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 System Volume Information Folders" and there are some other folders too which has the same behavior I cannot access them.

After studying about this Exception from Internet I have got to learn that after adding "Manifest File" and changing it option to "Require Administrator" to the program will run as a ADMINISTRATOR but unfortunately nothing as such happened. I am using some code which would allow me to run my code as a Administrator, but still showing me the same exception.

WindowsIdentity CurrentIdentity = WindowsIdentity.GetCurrent();
                WindowsPrincipal CurrentPrincipal = new      WindowsPrincipal(CurrentIdentity);

                if (CurrentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
//Scanning Code will Execute
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nikhil H Kuren
  • 31
  • 1
  • 1
  • 2

2 Answers2

4

You CANNOT give yourself admin privileges in code at all, that would be ridiculous! You must run visual studio as admin by RightClick->run as admin and your final application must be ran as admin as well.

use this: How do I force my .NET application to run as administrator?

in order to force the user to run as admin.

This won't make the application to work as admin but will promote user so that the application asks the user to run as admin during application launch

Martin
  • 3,396
  • 5
  • 41
  • 67
Nahum
  • 6,959
  • 12
  • 48
  • 69
  • Hi, Glad to see ur Reply, by the way The Solution which u gave me i tried it long back but still same issue. And i have to have give access to my programm any how, Because the Project itself called "Computer Scanner" so it has to scan each and every files in Computer. Can u show me any Other way in order to get access so that i can run my scanning code. – Nikhil H Kuren Dec 03 '12 at 07:14
  • @NikhilHKuren NO YOU CANNOT ITS IMPOSSBILE! none prviliged user cannot gain priviliged acess rights. – Nahum Dec 03 '12 at 10:19
  • Being Administrator if i dont get access on those folders then its quite shocking for me. I have one Question for you is, How Antivirus Company have access on those folders since they do not own that computer still they get access. How is this Posible? – Nikhil H Kuren Dec 03 '12 at 10:43
4

You can create the manifest using ClickOnce Security Settings, and then disable it:

Right click on the Project -> Properties -> Security -> Enable ClickOnce Security Settings

After you clicked it, a file will be created under the Project's properties folder called app.manifest once this is created, you can uncheck the Enable ClickOnce Security Settings option

Open that file and change this line :

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to:

 <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

This will make the program require administrator privileges.

Tahir Rehman
  • 332
  • 2
  • 9