Click to See Complete Forum and Search --> : Ensure I am always running from the correct folder [C# 2002]


Shaitan00
July 25th, 2007, 11:55 AM
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:


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,

crackersixx
July 25th, 2007, 12:43 PM
Most definately not the right approach.

You have to consider that a user may not have admin level access to the machine (unless you are the one controlling the machines/users) and therefore can't delete/move files.

Besides that, you should be writing your application such that it can operate wherever it is installed. I can't think of a reason where you would need a hardcoded path.

Shaitan00
July 25th, 2007, 12:59 PM
I am controlling the machine itself (so I am sure the access level is correct).
The idea is the application is standalone and should be run in the WORK folder - however a user can try to run it from anywhere if they have their own copy.

Now the complication comes in where this application (executable file) may need to update itself (the original copy) so it can't be IN USE when that happenes, so it is copied and ran from a temporary location (WORK) allowing me to update the original file (where the user ran it from) without worries...

In the end - I don't think there is another solution that will work under my environment - typically I provide manual steps to copy the file to work and run it there but I wanted to automated that so not to force the user to always copy/paste/run and let it be handled internally.

So - if you have another proposal to solve my issue I am all ears - otherwise I need to find a way for this to work without the flashing screen due to the form load... any ideas?

crackersixx
July 27th, 2007, 01:18 PM
Here's one way to accomplish this:

Loader.exe <-- this file checks for updates, and replaces other files as necessary.

Application.exe <- is the main app, which is launched by the loader after updating.

The user always runs Loader.exe to launch your application, which insures the latest version is running, then launches the main app.