views:

5033

answers:

7

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?

Thanks.

+10  A: 

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.

DannySmurf
Note that this is not a .NET limitation -- it's a general limitation of the User Account Control system.
Billy ONeal
+2  A: 

You may want to notify the user that the path is protected and ask them to output the file to a "safer" area. This way your app will not need elevation. I'm sure it depends on your users and what you are trying to do, however I don't think it's too much to kindly let the user know you don't feel ok dumping xyz into the Windows/System32 folder.

Brian Leahy
A: 

@Brian

Well UAC seems to protect random folders, I try to move things between folders on my secondary hard drive and it prompts me for permission. I really don't know what UAC is protecting and not protecting. I guess the solution is to just loop the FolderBrowserDialog until the path write comes back OK.

sieben
A: 

If your secondary drive has it's own file permissions, like say you have an other copy of windows installed on it. It will prompt.

It will also prompt if files are in use, which sometimes occurs if you have windows explorer open to the same directory and the file selected with a file previewer displaying the contents... there are some other oddities, but generally you get asked for file permission if the file is in use or it's a sensitive directory.

If you do loop the FolderBrowserDialog , make sure to notify the user why, so they dont get mad at your app.

Note: it does stink there is no .net way of asking for permission, maybe p/invoke the win32 api...?

Brian Leahy
P/Invoking wouldn't change anything; the rules for UAC are the same whether you're managed or native. It's COM, external process, or bust.
Chris Charabaruk
+13  A: 

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.

Aydsman
Nice reference to UAC demo, exactly what I needed. Thanks very much. Wish I could give more rep sometimes!
Ryan
@Ryan You're welcome. I'm a little frustrated how hard UAC seems to be in a managed environment. Feel free to browse my user profile and upvote my other questions! :)
Aydsman
BTW, the exception name is UnauthorizedAccessException with z in Unauthorized. Just that ;)
Matías
Thanks Matias. Fixed the post now.
Aydsman
A: 

UAC can elevate object based on their GUID, this would (In theory) mean that any class with a GUID can be elevated, The UACDemo should also show how to do this

Tristan
+1  A: 

Hi,

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

Thanks! Mosu'

mosu