[英]How do I test in C# if an exe is running correctly?
我有一個viewer.exe
在啟動時從“models”文件夾加載一些模型( *.mdl
)。 一些模型崩潰viewer.exe
:“viewer.exe已停止工作.Windows可以在線檢查問題的解決方案”。
我能做的是將所有.mdl
文件移動到“source”文件夾中,然后手動測試每個.mdl
文件移動到“models”如果viewer.exe
正在運行但是,有很多文件需要檢查。 如何將每個*.mdl
文件從“source”移動到“models”並以編程方式測試viewer.exe
是否正確運行?
這是我用於第一個問題的代碼:從“models”中的“source”文件.mdl files
移動.mdl files
。 有些文件名稱相同但大小不同:
String mask = "*.mdl";
String source = @"c:\Source\";
String destination = @"c:\Models\";
String[] files = Directory.GetFiles(source, mask, SearchOption.AllDirectories);
foreach (String file in files)
{
if (File.Exists(file) && !File.Exists(destination + new FileInfo(file).Name))
{
File.Move(file, destination + new FileInfo(file).Name);
}
else
{
FileInfo f = new FileInfo(file);
long s = f.Length;
FileInfo f2 = new FileInfo(destination + new FileInfo(file).Name);
long s2 = f2.Length;
if (s >= s2)
{
File.Delete(destination + new FileInfo(file).Name);
File.Move(file, destination + new FileInfo(file).Name);
}
}
}
使用process.start(startInfo)(參見http://msdn.microsoft.com/en-gb/library/0w4h05yb.aspx )
等待幾秒鍾,檢查進程是否已終止,然后返回process.hasexited( http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx )
然后使用process.kill()殺死它(參見http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx )
您可能需要關閉Windows錯誤報告: http : //msdn.microsoft.com/en-us/library/bb513638(VS.85).aspx
圍繞try-catch語句中可能失敗的操作
try {
File.Delete(destination + new FileInfo(file).Name);
} catch (Exception ex) {
// File could not be deleted
}
try {
File.Move(file, destination + new FileInfo(file).Name);
} catch (Exception ex) {
// File could not be moved
}
在catch語句中,如果無法處理文件,請執行您要執行的操作。
我已禁用Windows錯誤報告,這就是程序現在的樣子:
String mask = "*.mdl";
String source = @"c:\source\";
String destination = @"C:\Viewer\Models\";
String[] files = Directory.GetFiles(source, mask, SearchOption.AllDirectories);
foreach (String file in files)
{
if (File.Exists(file) && !File.Exists(destination + new FileInfo(file).Name))
{
File.Move(file, destination + new FileInfo(file).Name);
}
else
{
FileInfo f = new FileInfo(file);
long s = f.Length;
FileInfo f2 = new FileInfo(destination + new FileInfo(file).Name);
long s2 = f2.Length;
if (s >= s2)
{
File.Delete(destination + new FileInfo(file).Name);
File.Move(file, destination + new FileInfo(file).Name);
}
}
//mycompiledapp.exe is placed in Viewer folder for this to work
Process myprocess = Process.Start(@"viewer.exe");
Thread.Sleep(3000);
if (myprocess.HasExited) //Process crashes, exiting automatically
{
//Deletes the file that makes the viewer.exe crash
File.Delete(destination + new FileInfo(file).Name);
}
else
{
myprocess.Kill();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.