3

I have created a program in C# that allows you to change the background of the login screen of Windows 7.

The program has to create a new folder in the System32 folder, and also move a file there. I have no problem with this on my personal machines, but I tested it on a friends machine and it refused to create the directory.

His account type was administrator, but the only thing that I can think of is that he is missing some sort of privilege that I have enabled on my computer.

So I was wondering if there was a way to check what permissions the user has? Or a way to go around it. Thanks in advance!

Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
Milkboat
  • 179
  • 1
  • 6
  • http://stackoverflow.com/questions/1089046/in-net-c-test-if-user-is-an-administrative-user ,http://stackoverflow.com/questions/3600322/check-if-the-current-user-is-administrator possible duplicate's – Rosmarine Popcorn Nov 14 '11 at 21:20
  • http://www.davidmoore.info/2011/06/20/how-to-check-if-the-current-user-is-an-administrator-even-if-uac-is-on/ – Hans Passant Nov 14 '11 at 22:32

2 Answers2

3

To detect this you can get an object of WindowsIdentity like this:

WindowsIdentity identity = WindowsIdentity.GetCurrent();

Then create an instance of WindowsPrincipan by :

WindowsPrincipal principal = new WindowsPrincipal(identity);

and finally check it by using IsInRole() method like this:

string role = "BUILTIN\\Administrators";
bool IsAdmin = principal.IsInRole(role));

then you can use the IsAdmin variable to determine whether the current user is an Admin or not.

source - http://csharptuning.blogspot.com/2007/09/detecting-is-current-user-is.html

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
2

The issue is UAC. This is an operation that requires elevation. Although the user is in the administrators group, the process is given a standard user token by default.

The solution is to add the requireAdministrator option to your application's manifest so that the application invokes the UAC elevation dialog.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490