views:

38

answers:

1

so i have this code to extract a program to the temp directory then run it, the problem is it doesn't work perfectly on every computer (for some reason it hits a error or exception sometimes)

string tempFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "");
System.Diagnostics.Process defrag1 = System.Diagnostics.Process.Start(@"Programs\Optimize\AusLogics_Defrag.exe", string.Format(" -o{0} -y", tempFolder));
defrag1.WaitForExit();
string executableDirectoryName = Path.GetDirectoryName(Application.ExecutablePath);
System.Diagnostics.Process defrag2 = System.Diagnostics.Process.Start(tempFolder + "\\" + "AusLogics_Defrag" + "\\" + "DiskDefrag.exe", "");
defrag2.WaitForExit();
System.IO.Directory.Delete(tempFolder + "\\" + "AusLogics_Defrag", true);

and what i wanna know is there a way that say if it starts to extract but hits a error (no matter what it is) it will automatically change and go to this code, but if it DOESN'T hit a error it continues as it was meant to?

string tempFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
System.Diagnostics.Process defrag1 = System.Diagnostics.Process.Start(@"Programs\Optimize\AusLogics_Defrag.exe", string.Format(" -o{0} -y", tempFolder));
defrag1.WaitForExit();
string executableDirectoryName = Path.GetDirectoryName(Application.ExecutablePath);
System.Diagnostics.Process defrag2 = System.Diagnostics.Process.Start(tempFolder + "\\" + "AusLogics_Defrag" + "\\" + "DiskDefrag.exe", "");
defrag2.WaitForExit();
System.IO.Directory.Delete(tempFolder + "\\" + "AusLogics_Defrag", true);

with the path going to the application data folder? and if THAT hits a error it would change that path to this, but if it DOESN'T hit a error it continues as it was meant to?

string tempFolder = System.Environment.GetEnvironmentVariable("HomeDrive");
A: 

I guess your answer really depends how your errors are bubbled out of the 'AusLogics_Defrag.exe' app...

Providing the 'AusLogics_Defrag.exe' app outputs one, you could work with the ExitCode property of the process returned when you call Process.Start(). A non-zero value indicates an error in a normal implementation.

Reddog
@Reddog well the problem is that when it trys to extract to the TEMP folder (which is the environment variable temp folder) sometimes it gets a file not found error.. so in that case id want it to then try to extract to the applications data folder, and if a error occurs there then id want it to try to extract to the Homedrive environment variable
NightsEVil