开发者

How to Make my application handle Errors for a few different scenarios?

开发者 https://www.devze.com 2023-01-03 19:52 出处:网络
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)

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.WaitFor开发者_开发百科Exit();
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");


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.

0

精彩评论

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

关注公众号