Ensure I am always running from the correct folder [C# 2002]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
When a user launches my application the first thing I want to do is check to ensure I am in the correct folder, if so just continue, but if not I need to copy myself to that folder, start myself in that folder, and close the current instance of myself... All this needs to happen without the user "seeing" anything like window flashing, etc... So far I have the current code:

Code:
if (Application.ExecutablePath.ToUpper() != sWorkPath)
{
	if (File.Exists(sWorkPath))
		File.Delete(sWorkPath);
	File.Copy(Application.ExecutablePath, sWorkPath, true);
                    
	Process pProcess = new System.Diagnostics.Process();
	pProcess.StartInfo.FileName = sWorkPath;
	pProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
	pProcess.Start();

	Application.Exit();
}

So this should:
- Check to ensure I am running in the WORK folder
- If not then copy myself to WORK
- Start a process of myself in WORK
- Close myself
All without any flashing - so I run this as the first step of the form load (frmMain_Load) as any point before that Application.Exit() (also tried
this.Close();) doesn't seem to work at all...
But sadly this still causes some "flashing" to occur as I assume this is because I am running this code in the FORM_LOAD event so the form is being drawn and then exited ... I need to ensure no flashing occurs ...

Questions are simple - is this the right approach to solve my problem and if not can you recommend something better?
Is this the right place to run this code (begining of the Form Load) or could you offer a better, possibly earlier, location where this can/should be run to avoid this flashing issue I am having...
What is the best way to ensure I end this current running process? this.Close();? Application.Exit();?

Any help would be greatly appreciated... if there are other better ways to do this also... any suggestions would help (I just thought this up as a solution)
Thanks,
 
Like you said, the problem is that you are running this code in Form_Load, when the Form object has already been created and handles allocated. (I also don't recommend using Application.Exit; if this.Close doesn't work, it is an indicator that something else is wrong with your approach.) This code should be executed in your application's Main function.
 
Back
Top