views:

158

answers:

3

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?

+6  A: 

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).

Anon.
Did you actually read what I wrote?? I already have ascertained whether the necessary folder exists or not, I just need to know how to stop the app from loading!What is the function name that actually stops the app from loading, or what is the function name necessary to unload the application?
flavour404
Did you actually try the code I posted? Because it works. If you think otherwise, you're wrong. Returning from the `Main()` method exits the application.
Anon.
Ok, sorry, I understand a little better what you are talking about. You say 'replace symbolic names...' what would be the appropriate replacement for 'ERROR_FOLDER_NOT_EXIST?'
flavour404
You could just `return;` since the chances are your `Main()` is defined as `static void Main()` and expects no value to be returned.
Cory Charlton
Cory, you are indeed correct.
flavour404
+1 @Anon Is it crucial to put your test for file-folder-exists code and exit-case as the first statements in Main in Program.cs ? My experiment shows that does work, thanks.
BillW
It is necessary to put it before the `Application.Run()` line. Other than that, it doesn't really matter.
Anon.
+2  A: 

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.

Wagner Silveira
A: 

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

Regina