0

viewer.exe起動時*.mdlに「models」フォルダからいくつかのモデル()をロードするものがあります。一部のモデルがクラッシュしviewer.exeます:「viewer.exeが動作を停止しました。Windowsは問題の解決策をオンラインで確認できます」。
私にできることは.mdl、「ソース」フォルダー内のすべてのファイルを移動し、実行中の.mdl場合は「モデル」に移動された各ファイルを手動でテストするviewer.exeことですが、チェックするファイルはたくさんあります。各*.mdlファイルを「ソース」から「モデル」に移動し、viewer.exeが正しく実行されているかどうかをプログラムでテストするにはどうすればよいですか?

これが私の最初の問題に使用するコードです:.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);
        }
    }
}
4

3 に答える 3

1

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

于 2013-02-08T18:41:53.963 に答える
1

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ステートメントで、ファイルを処理できなかった場合に備えて、やりたいことをすべて実行します。

于 2013-02-08T18:53:09.883 に答える
0

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();
        }
       }
于 2013-02-09T10:27:27.893 に答える