开发者

How to stop a .NET application from loading if certain criteria aren't met

开发者 https://www.devze.com 2022-12-19 05:55 出处:网络
I need certain criteria to be met, a particular folder needs to be present on the c drive, before this app should load. How, during the load process, can I stop/quit the application.

I need certain criteria to be met, a particular folder needs to be present on the c drive, before this app should load. How, during the load process, can I stop/quit the application.

I already have ascertained if the folder exists. In old VB projects you would just use 'Unload Me' but that isn'开发者_如何学编程t in C#. I tried Application.Exit() but that doesn't work under these circumstances and the app still loads, how do I stop it?


Open up Program.cs. In there you'll find you Main() method.

In there, put something like:

if (FolderDoesNotExist())
    return ERROR_FOLDER_NOT_EXIST;

(replacing those symbolic names with other stuff as appropriate).


I would create an initialization function that would be the first item to be called from Main(). Depending on your output and how long your initialization takes you can even use a splash window to inform the user about progress. Once all initialization is completed, you can decide if you start the app or not.


// In the main initialization of the main form (in XXX.Designer.cs file InitializeComponent(), for example)

this.Load += new System.EventHandler(this.CheckProcesses);

// The CheckProcess method

private void CheckProcesses(object sender, EventArgs e)

{ try { if (SomethingIsWrongWithThatFolder()) this.Close(); } catch { } }

// This will shut the process of your app before the UI actually loads. So, your user doesn't see anything at all

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号