How to handle UnauthorizedAccessException from User Code
Asked Answered
L

3

6

In a Windows Application I have, I am doing some changes in registry like deleting a particular key, in some test scenarios like in a Vista machine with its UAC put on, I'm getting System.UnauthorizedAccessException. My code would look something like this:

try
{
    //delete registry keys
}
catch (UnauthorizedAccessException ex)
{
    //handling
}
catch (Exception genEx)
{
    //handling
}

But the application would still go crashing., not being handled by the catch block. Is there some way I could handle it?

Loreanloredana answered 23/12, 2011 at 15:4 Comment(6)
Are you sure the code that is causing your application to crash is in the try statement?Clypeus
codeproject.com/KB/vista-security/MakingAppsUACAware.aspxDenisse
You must be able to catch this exception. Maybe you could give us more code (I mean at least the call that throws the exception) to help fix it.Estebanesteem
Sorry guys. It was a mistake from my side. I did logging to a location I did not have access, in catch block also I had the logging. Once again sorry and moreover thanks.Loreanloredana
I have a similar issue on first catch I "throw ex;" But then it reaches general Exeption(the second one,genEx) and says Unhandled UnauthorizedAccessException. It is a base of my domain service classes.The Session is dead but what I want to do is learning is session dead?Club
@nemnem if a moderator will undelete your answer I think it could be marked as accepted. Otherwise you may post a new answer.Elkin
L
1

You're probably throwing another exception from the catch block. Try commenting all lines in the catch block and it should work just fine.

Leeds answered 7/2, 2014 at 16:22 Comment(2)
+1 please elaborate little bit, finally this was correct answer and @niranjan may/should accept it!Elkin
I had found this issue on the day of posting the question and added it as a comment... I had forgotten to mark it as answered.. :( sorry guys... if it caused confusion :) @Adriano: Thanks for reminding.. I'll mark this as the answer.. :)Loreanloredana
S
0

Most common and obvious reason is that the path / file program is trying access doesn't have access to the identity under which its running.

Read more here on MSDN

Septilateral answered 8/3, 2014 at 7:9 Comment(3)
why -1 ? can someone speak up to that ?Septilateral
+1.. Yes that was the issue.. I had added a comment in my question itself.. though I didn't add as answer and mark it :(..Loreanloredana
I didn't notice comment but marking may help another user for finding right answer..Septilateral
D
0

you have to set admin access for your application

for that just right click and select properties

and check Run as Administrator check and run your application

to run as administrator for all users click on all user button and select run as administrator for all users this will always run your application with admin access for all users

enter image description here

Diphenyl answered 8/3, 2014 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.