If a .NET console application is built in the VisualStudio with project settings Security
> Enable ClickOnce security settings
> This is a partial trust application
, the following ways to exit application throw SecurityException
due to insufficient privileges because they cannot access the system environment:
Environment.Exit()
Environment.FailFast()
End
statement (Visual Basic)How to exit the application without running into this exception?
More details:
Exception message is: Message=Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
.
This is perfectly OK, as they obviously want to return an ErrorLevel
, but the system environment is not accessible at that time. But do you know about any way to prematurely exit the application which does not attempt to access the environment and thus is able to exit without triggering the exception?
The only working way I found so far is to continue the execution to end of Main()
where the application exits 'naturally'. But this sometimes adds unwanted code constructs. This is why I'm asking how to exit immediately.
Please, in this case let's not conclude by adding privileges to the application etc. Let's keep the application restricted as it is and find a way how to exit it. Does .NET offer some other way to exit the app instantly than the above three?
did you try
Process.GetCurrentProcess().Kill()
or
AppDomain.Unload(AppDomain.CurrentDomain)
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With