Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request Windows Vista UAC elevation if path is protected?

For my C# app, I don't want to always prompt for elevation on application start, but if they choose an output path that is UAC protected then I need to request elevation.

So, how do I check if a path is UAC protected and then how do I request elevation mid-execution?

like image 638
sieben Avatar asked Aug 20 '08 07:08

sieben


3 Answers

The best way to detect if they are unable to perform an action is to attempt it and catch the UnauthorizedAccessException.

However as @DannySmurf correctly points out you can only elevate a COM object or separate process.

There is a demonstration application within the Windows SDK Cross Technology Samples called UAC Demo. This demonstration application shows a method of executing actions with an elevated process. It also demonstrates how to find out if a user is currently an administrator.

like image 134
Adrian Clark Avatar answered Oct 21 '22 18:10

Adrian Clark


Requesting elevation mid-execution requires that you either:

  1. Use a COM control that's elevated, which will put up a prompt
  2. Start a second process that is elevated from the start.

In .NET, there is currently no way to elevate a running process; you have to do one of the hackery things above, but all that does is give the user the appearance that the current process is being elevated.

The only way I can think of to check if a path is UAC elevated is to try to do some trivial write to it while you're in an un-elevated state, catch the exception, elevate and try again.

like image 45
TheSmurf Avatar answered Oct 21 '22 18:10

TheSmurf


I'm not sure if it is of any help for you but you can take a look at this blog post:

http://haishibai.blogspot.com/2010/01/tiy-try-out-windows-7-uac-using-c-part_26.html

like image 4
mosu Avatar answered Oct 21 '22 19:10

mosu