Catch security exception

  • Thread starter Thread starter William Stacey
  • Start date Start date
W

William Stacey

How do you catch the security exception that may occur when running .Net
from a network drive and you don't have the proper Trust level? TIA. Get
this messagebox that I can't seem to catch:

Common Language Runtime Debugging Servi...
Application has generated an exception that could not be handled.
Process id=blah, Thread id=...
Click OK to terminate the application.
Click CANCEL to debug the application.
 
Hi William,

As I understand, you want to catch the securityexception in your
application which occur when running .Net from a network drive. Besides
Try..Catch statement, we also can set a Application's OnThreadException to
catch an unexpect exception:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformsapplicationclassonthreadexceptiontopic.asp

It is worthy metioned that this only work when you run the executable file.
If you run the application from VS.NET IDE, it doesn't work since VS.NET
IDE already handled it.

Hope this help,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Thanks. I tried the try catch and the OnThreadException and can't seem to
catch this security exception. Me thinks it is caught before main is even
entered. Can you think of other? TIA
 
Hi William,

Regarding on this issue, you may assert all permission required by the
application first in the main sub. For exmaple, if the application will
open a file:

Dim fileIOPerm1 As New FileIOPermission(FileIOPermissionAccess.AllAccess,
"C:\testio.txt")

Try
fileIOPerm1.Assert()
Catch ex As Security.SecurityException

Console.WriteLine(ex.Message)

End Try

If the assembly didn't have the permission, a exception will be generated
and this exception can be caught by the try statement.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top