0

I'm trying to delete a folder off the desktop when I get the error that the path is denied. I've even made my app.manifest start the program as administrator. Now with the code below I can delete the folder successfully, but it seems so unnecessary.

  string folder = "c:\\users\\jim\\desktop\\Proto";
  ProcessStartInfo process = new ProcessStartInfo("cmd.exe", @"/C " + @"rmdir " + folder + " /s /q");
  process.Verb = "runas";
  Process p = Process.Start(process);
  p.WaitForExit();

Im trying to use ...

  string folder = "c:\\users\\jim\\desktop\\Proto";
  Directory.Delete(folder, true);

Is it possible to to make that method "runas"?

JimDel
  • 4,309
  • 11
  • 54
  • 99

3 Answers3

2

As far as I know it's impossible to elevate just a single call/command. You'll have to launch a new process that might get blocked by UAC. Some programs seem to do so otherwise (e.g. different Windows dialogs), but they just call another process (or their own executable) with different parameters in background.

Besides that: By default, you should have full access to everything on your own desktop. Or are you trying to modify some other user's desktop?

Mario
  • 35,726
  • 5
  • 62
  • 78
1

Well for a start, the user running the application should be able to create a folder on their own desktop.

If the above is the case get rid of the stuff you put in the manifest but don't use a hard coded path

String desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

Directory.Delete(Path.Combine(desktopFolder,"proto");

If this folder is being deleted for another other user (and admin is another user, elevated or not), then you have to run with elevated all the time, (not a good idea) , or as you are kick off another process with elevated rights to do the elevated stuff.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • I am actually using "Environment.SpecialFolder.DesktopDirectory" in my code. I just left it out for my question for readability. – JimDel Dec 21 '12 at 17:27
  • Must be something else you haven't mentioned then..... You can presumably delete folder on your desktop yourself, as in right-click -> Delete? – Tony Hopkinson Dec 21 '12 at 17:38
0

Take a look at this, it shows you how to run a single method as Admin user

Community
  • 1
  • 1
Jacques Bronkhorst
  • 1,685
  • 6
  • 34
  • 64
  • No it doesn't. You need to start a new process to elevate. – David Heffernan Dec 21 '12 at 17:19
  • That marks up a method as requiring elevate so you can do clever stuff, it doesn't actually elevate though. One of the biggest windows security improvements, was stopping in-process elevation, it's what all the bad guys were using. – Tony Hopkinson Dec 21 '12 at 17:35