0

我有一个在启动时从“模型”文件夹viewer.exe加载一些模型( )。*.mdl部分机型崩溃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

包围在 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 回答
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 回答
0

I have disabled windows error reporting and this is how the program looks like now:

        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 回答