<p>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.</p>

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

[{'answer_id': 17544, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 4, 'selected': False, 'text': "<p>Requesting elevation mid-execution requires that you either:</p>\n\n<ol>\n<li>Use a COM control that's elevated, which will put up a prompt</li>\n<li>Start a second process that is elevated from the start.</li>\n</ol>\n\n<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 17557, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 17701, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': -1, 'selected': False, 'text': "<p>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.</p>\n\n<p>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. </p>\n\n<p>If you do loop the FolderBrowserDialog , make sure to notify the user why, so they dont get mad at your app.  </p>\n\n<p>Note: it does stink there is no .net way of asking for permission, maybe p/invoke the win32 api...?</p>\n"}, {'answer_id': 90442, 'author': 'Adrian Clark', 'author_id': 148, 'author_profile': 'https://Stackoverflow.com/users/148', 'pm_score': 6, 'selected': True, 'text': '<p>The best way to detect if they are unable to perform an action is to attempt it and catch the <code>UnauthorizedAccessException</code>.</p>\n\n<p>However as @<a href="https://stackoverflow.com/users/941/dannysmurf">DannySmurf</a> <a href="https://stackoverflow.com/questions/17533/request-vista-uac-elevation-if-path-is-protected#17544">correctly points out</a> you can only elevate a COM object or separate process.</p>\n\n<p>There is a demonstration application within the Windows SDK Cross Technology Samples called <a href="http://msdn.microsoft.com/en-us/library/aa970890.aspx" rel="nofollow noreferrer" title="MSDN - UAC Sample">UAC Demo</a>.  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.</p>\n'}, {'answer_id': 1819065, 'author': 'Tristan', 'author_id': 221263, 'author_profile': 'https://Stackoverflow.com/users/221263', 'pm_score': -1, 'selected': False, 'text': '<p>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</p>\n'}, {'answer_id': 2773771, 'author': 'mosu', 'author_id': 64212, 'author_profile': 'https://Stackoverflow.com/users/64212', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m not sure if it is of any help for you but you can take a look at this blog post:</p>\n\n<p><a href="http://haishibai.blogspot.com/2010/01/tiy-try-out-windows-7-uac-using-c-part_26.html" rel="nofollow noreferrer">http://haishibai.blogspot.com/2010/01/tiy-try-out-windows-7-uac-using-c-part_26.html</a></p>\n'}]
['https://Stackoverflow.com/questions/17533', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1147/']