-1

编辑:如果有人想测试,这里是文件: Cleaner

出于性能原因,我正在制作一个在启动时自动清理一些文件的程序。

如何使应用程序在另一台只有发布文件的计算机上运行?

我使用 Framework 2.0 制作了应用程序,并且在我的计算机上正常工作且没有错误,但是如果我将发布文件复制到另一台计算机,则在启动时会发生“应用程序停止响应”。

没有来自 Icon 的资源文件,并且是嵌入式资源。我不知道该怎么做。

    private void Cleansystem()
    {
        timer1.Enabled = true;
        timer1.Start();
        progressBar1.Maximum = 10;
        String Offline = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%") + "\\Offline Web Pages";
        String download = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%") + "\\Downloaded Program Files";
        String software = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%") + "\\SoftwareDistribution\\Download";
        String wintemp = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%") + "\\Temp";
        String recent = Environment.ExpandEnvironmentVariables("%appdata%") + "\\Microsoft\\Windows\\Recent";
        String cookies = Environment.ExpandEnvironmentVariables("%appdata%") + "\\Microsoft\\Windows\\cookies";
        String TempInternet = Environment.ExpandEnvironmentVariables("%userprofile%") + "\\Local Settings\\Temporary Internet Files";
        String apptemp = Environment.ExpandEnvironmentVariables("%userprofile%") + "\\appdata\\Local\\Temp";
        String settingtemp = Environment.ExpandEnvironmentVariables("%userprofile%") + "\\Local Settings\\Temp";
        EmptyFolderContents(Offline);
        EmptyFolderContents(download);
        EmptyFolderContents(software);
        EmptyFolderContents(wintemp);
        EmptyFolderContents(recent);
        EmptyFolderContents(cookies);
        EmptyFolderContents(TempInternet);
        EmptyFolderContents(apptemp);
        EmptyFolderContents(settingtemp);
        timer1.Interval = 1000;
        timer1.Tick += new EventHandler(timer1_Tick);
    }

    private void EmptyFolderContents(string folderName)
    {
        foreach (var folder in Directory.GetDirectories(folderName))
        {
            try
            {
                Directory.Delete(folder, true);
            }
            catch (Exception excep)
            {
                logger.Fatal(excep);
            }
        }
        foreach (var file in Directory.GetFiles(folderName))
        {
            try
            {
                File.Delete(file);
            }
    catch (Exception excep)
    {
      logger.Fatal(excep);
    }
        }
    }
    void timer1_Tick(object sender, EventArgs e)
    {
        if (progressBar1.Value != 10)
        {
            progressBar1.Value++;
        }
        else
        {
            Environment.Exit(0);
        }
    }
}

}

2013-05-08 18:02:58.7013 致命 System.IO.IOException:进程无法访问文件“C:\Windows\Temp\Cleaner.vsho_130508_173905_05efe1ff-6b41-4cb6-b​​b00-7acad6960c36.iTrace”,因为它正在被使用通过另一个过程。在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径)
在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012 \Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:58.7349 FATAL System.IO.IOException:进程无法访问文件'C:\Windows\Temp\OSO。 vshost.exe_130508_174608_0d66d9df-040c-457b-84b5-7c3576196718.iTrace',因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径)
在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012 \Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:58.8630 FATAL System.UnauthorizedAccessException:访问路径“Aug2005_d3dx9_27_x64.cab”被拒绝。在 System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost) at System.IO.Directory.Delete (String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound) at System.IO.Directory.Delete (字符串路径,布尔递归)在 C 中的 GifImagee.MainWindow.EmptyFolderContents(String folderName):\Users\Administrator\Documents\Visual Studio 2012\Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 912 2013-05-08 18:02:58.8743 FATAL System.IO.IOException: The process cannot access the file 'C:\Users\Administrator\appdata\Local\Temp\etilqs_2Isw34P5eLyyP6D' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径)
在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012 \Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:58.9074 FATAL System.IO.IOException: The process cannot access the file 'C:\Users\Administrator\appdata\ Local\Temp\etilqs_7CSWlOdfR5GbUuD' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径)
在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012 \Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:58.9279 FATAL System.IO.IOException: The process cannot access the file 'C:\Users\Administrator\appdata\ Local\Temp\etilqs_9htGcbvUEEpPsK3' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径)
在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012 \Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:58.9389 FATAL System.IO.IOException: The process cannot access the file 'C:\Users\Administrator\appdata\ Local\Temp\etilqs_fPoiWPUjDlCkgey' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径)
在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012 \Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:58.9703 FATAL System.IO.IOException: The process cannot access the file 'C:\Users\Administrator\appdata\ Local\Temp\etilqs_JtJ95QHTNu6Vj7D' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径)
在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012 \Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:59.0947 FATAL System.UnauthorizedAccessException:访问路径“Aug2005_d3dx9_27_x64.cab”被拒绝。在 System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost) at System.IO.Directory.Delete (String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound) at System.IO.Directory.Delete (字符串路径,布尔递归)在 C 中的 GifImagee.MainWindow.EmptyFolderContents(String folderName):\Users\Administrator\Documents\Visual Studio 2012\Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 912 2013-05-08 18:02:59.1152 FATAL System.IO.IOException: The process cannot access the file 'C:\Users\Administrator\Local Settings\Temp\etilqs_2Isw34P5eLyyP6D' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.InternalDelete(String path, Boolean checkHost) 在 System.IO.File.Delete(String path) 在 GifImagee.MainWindow.EmptyFolderContents(String folderName) 在 c:\Users\Administrator\Documents\Visual Studio 2012\Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:59.1263 FATAL System.IO.IOException: The进程无法访问文件'C: \Users\Administrator\Local Settings\Temp\etilqs_7CSWlOdfR5GbUuD' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.InternalDelete(String path, Boolean checkHost) 在 System.IO.File.Delete(String path) 在 GifImagee.MainWindow.EmptyFolderContents(String folderName) 在 c:\Users\Administrator\Documents\Visual Studio 2012\Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:59.1563 FATAL System.IO.IOException: The进程无法访问文件“C:\Users\Administrator\Local Settings\Temp\etilqs_9htGcbvUEEpPsK3”,因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GifImagee.MainWindow.EmptyFolderContents(String folderName) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\tet\Cleaner\Cleaner\MainWindow.xaml .cs:line 923 2013-05-08 18:02:59.1774 FATAL System.IO.IOException: 进程无法访问文件'C:\Users\Administrator\Local Settings\Temp\etilqs_fPoiWPUjDlCkgey',因为它正在被另一个人使用过程。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.InternalDelete(String path, Boolean checkHost) 在 System.IO.File.Delete(String path) 在 GifImagee.MainWindow.EmptyFolderContents(String文件夹名称)在 c:\Users\Administrator\Documents\Visual Studio 2012\Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923 2013-05-08 18:02:59。1886 FATAL System.IO.IOException:进程无法访问文件“C:\Users\Administrator\Local Settings\Temp\etilqs_JtJ95QHTNu6Vj7D”,因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.InternalDelete(String path, Boolean checkHost) 在 System.IO.File.Delete(String path) 在 GifImagee.MainWindow.EmptyFolderContents(String文件夹名称)在 c:\Users\Administrator\Documents\Visual Studio 2012\Projects\tet\Cleaner\Cleaner\MainWindow.xaml.cs:line 923

4

3 回答 3

1

我将尝试以这种方式更改您的代码

private void EmptyFolderContents(string folderName)
{
    if(Directory.Exists(folderName)
    {
        foreach (var folder in Directory.GetDirectories(folderName))
        {
            try
            {
                Directory.Delete(folder, true);
            }
            catch(Exception ex)                
            {
                 MessageBox.Show("Error deleting folder: " + folder+ Environment.NewLine + ex.Message);
            }
        }
        foreach (var file in Directory.GetFiles(folderName))
        {
            try
            {
                File.Delete(file);
            }
            catch(Exception ex)
            {
                 MessageBox.Show("Error deleting file: " + file + Environment.NewLine + ex.Message);
            }
        }

    }

}

我添加了最小检查以查看目录是否存在,但请记住,此代码可能由于多种原因而失败。查看MSDN 文档中的例外列表

于 2013-05-08T16:01:44.740 回答
0

您是否查看过“事件查看器”以查看那里是否有更多详细信息。可能缺少配置文件或遇到权限问题。

于 2013-05-08T15:46:40.770 回答
0

好的,其中一个文件已锁定 - 这对于我想是您要删除的临时文件来说很常见。

这不应该导致您的问题,因为删除是在尝试捕获中,老实说 - 在您的情况下,最好有一个空的尝试捕获。

看这里。

检测文件是否被另一个进程(或实际上是同一个进程)锁定

当您尝试访问以下文件夹“Aug2005_d3dx9_27_x64.cab”时出现安全异常,您会收到“FATAL System.UnauthorizedAccessException: Access to the path x”错误。

尝试将您的 'foreach (var folder in Directory.GetDirectories(folderName))' 块包装在 try catch 中,更新文件夹权限,从删除中排除有问题的文件夹或检查文件夹的权限,然后再尝试访问它们。

于 2013-05-08T16:21:50.887 回答