C#使应用程序与其他计算机兼容吗?

时间:2013-05-08 15:44:50

标签: c#

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

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

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

我使用Framework 2.0制作了应用程序,并且在我的计算机上正常运行但是如果我将发布文件复制到另一台计算机,那么#34;应用程序停止响应"在它开始时发生。

除了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 FATAL System.IO.IOException:进程   无法访问该文件   ' C:\ WINDOWS \ TEMP \ Cleaner.vsho_130508_173905_05efe1ff-6b41-4cb6-b​​b00-7acad6960c36.iTrace'   因为它正被另一个进程使用。在   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:第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 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:第923行   2013-05-08 18:02:58.8630 FATAL System.UnauthorizedAccessException:   访问路径' Aug2005_d3dx9_27_x64.cab'被拒绝。在   System.IO.Directory.DeleteHelper(String fullPath,String userPath,   布尔递归,布尔throwOnTopLevelDirectoryNotFound)at   System.IO.Directory.Delete(String fullPath,String userPath,Boolean   System.IO.Directory.Delete(String。的递归,布尔检查)   path,Boolean recursive)at   GifImagee.MainWindow.EmptyFolderContents(String folderName)in   c:\ Users \ Administrator \ Documents \ Visual Studio   2012 \ Projects \ tet \ Cleaner \ Cleaner \ MainWindow.xaml.cs:第912行   2013-05-08 18:02:58.8743 FATAL System.IO.IOException:进程   无法访问该文件   ' C:\用户\管理员\应用程序数据\本地\ TEMP \ etilqs_2Isw34P5eLyyP6D'   因为它正被另一个进程使用。在   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:第923行   2013-05-08 18:02:58.9074 FATAL System.IO.IOException:进程   无法访问该文件   ' C:\用户\管理员\应用程序数据\本地\ TEMP \ etilqs_7CSWlOdfR5GbUuD'   因为它正被另一个进程使用。在   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:第923行   2013-05-08 18:02:58.9279 FATAL System.IO.IOException:进程   无法访问该文件   ' C:\用户\管理员\应用程序数据\本地\ 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:第923行   2013-05-08 18:02:58.9389 FATAL System.IO.IOException:进程   无法访问该文件   ' C:\用户\管理员\应用程序数据\本地\ TEMP \ etilqs_fPoiWPUjDlCkgey'   因为它正被另一个进程使用。在   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:第923行   2013-05-08 18:02:58.9703 FATAL System.IO.IOException:进程   无法访问该文件   ' C:\用户\管理员\应用程序数据\本地\ TEMP \ etilqs_JtJ95QHTNu6Vj7D'   因为它正被另一个进程使用。在   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:第923行   2013-05-08 18:02:59.0947致命System.UnauthorizedAccessException:   访问路径' Aug2005_d3dx9_27_x64.cab'被拒绝。在   System.IO.Directory.DeleteHelper(String fullPath,String userPath,   布尔递归,布尔throwOnTopLevelDirectoryNotFound)at   System.IO.Directory.Delete(String fullPath,String userPath,Boolean   System.IO.Directory.Delete(String。的递归,布尔检查)   path,Boolean recursive)at   GifImagee.MainWindow.EmptyFolderContents(String folderName)in   c:\ Users \ Administrator \ Documents \ Visual Studio   2012 \ Projects \ tet \ Cleaner \ Cleaner \ MainWindow.xaml.cs:第912行   2013-05-08 18:02:59.1152 FATAL System.IO.IOException:进程   无法访问文件' C:\ Users \ Administrator \ Local   设置\ TEMP \ etilqs_2Isw34P5eLyyP6D'因为它正被使用   另一个过程。在System.IO .__ Error.WinIOError(Int32 errorCode,   System.IO.File.InternalDelete(String path,String MaybeFullPath)   布局checkHost)在System.IO.File.Delete(String path)处   GifImagee.MainWindow.EmptyFolderContents(String folderName)in   c:\ Users \ Administrator \ Documents \ Visual Studio   2012 \ Projects \ tet \ Cleaner \ Cleaner \ MainWindow.xaml.cs:第923行   2013-05-08 18:02:59.1263 FATAL System.IO.IOException:进程   无法访问文件' C:\ Users \ Administrator \ Local   设置\温度\ etilqs_7CSWlOdfR5GbUuD'因为它正被使用   另一个过程。在System.IO .__ Error.WinIOError(Int32 errorCode,   System.IO.File.InternalDelete(String path,String MaybeFullPath)   布局checkHost)在System.IO.File.Delete(String path)处   GifImagee.MainWindow.EmptyFolderContents(String folderName)in   c:\ Users \ Administrator \ Documents \ Visual Studio   2012 \ Projects \ tet \ Cleaner \ Cleaner \ MainWindow.xaml.cs:第923行   2013-05-08 18:02:59.1563 FATAL System.IO.IOException:进程   无法访问文件' C:\ Users \ Administrator \ Local   设置\ TEMP \ etilqs_9htGcbvUEEpPsK3'因为它正被使用   另一个过程。在System.IO .__ Error.WinIOError(Int32 errorCode,   System.IO.File.InternalDelete(String path,String MaybeFullPath)   布局checkHost)在System.IO.File.Delete(String path)处   GifImagee.MainWindow.EmptyFolderContents(String folderName)in   c:\ Users \ Administrator \ Documents \ Visual Studio   2012 \ Projects \ tet \ Cleaner \ Cleaner \ MainWindow.xaml.cs:第923行   2013-05-08 18:02:59.1774 FATAL System.IO.IOException:进程   无法访问文件' C:\ Users \ Administrator \ Local   设置\ TEMP \ etilqs_fPoiWPUjDlCkgey'因为它正被使用   另一个过程。在System.IO .__ Error.WinIOError(Int32 errorCode,   System.IO.File.InternalDelete(String path,String MaybeFullPath)   布局checkHost)在System.IO.File.Delete(String path)处   GifImagee.MainWindow.EmptyFolderContents(String folderName)in   c:\ Users \ Administrator \ Documents \ Visual Studio   2012 \ Projects \ tet \ Cleaner \ Cleaner \ MainWindow.xaml.cs:第923行   2013-05-08 18:02:59.1886 FATAL System.IO.IOException:进程   无法访问文件' C:\ Users \ Administrator \ Local   设置\ TEMP \ etilqs_JtJ95QHTNu6Vj7D'因为它正被使用   另一个过程。在System.IO .__ Error.WinIOError(Int32 errorCode,   System.IO.File.InternalDelete(String path,String MaybeFullPath)   布局checkHost)在System.IO.File.Delete(String path)处   GifImagee.MainWindow.EmptyFolderContents(String folderName)in   c:\ Users \ Administrator \ Documents \ Visual Studio   2012 \ Projects \ tet \ Cleaner \ Cleaner \ MainWindow.xaml.cs:923行

3 个答案:

答案 0 :(得分: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文档中的Look at the exceptions list

答案 1 :(得分:0)

您是否查看了“事件查看器”以查看是否有更多详细信息。可能缺少配置文件或命中权限问题。

答案 2 :(得分:0)

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

这不应该导致你的问题,因为删除是在try catch中,老实说 - 在你的情况下,最好有一个空的try catch。

见这里。

Detecting whether a file is locked by another process (or indeed the same process)

当您尝试访问以下文件夹时出现安全性异常'Aug2005_d3dx9_27_x64.cab'您收到'FATAL System.UnauthorizedAccessException:访问路径x'错误。

尝试在try catch中包装你的'foreach(Directory.GetDirectories(folderName)中的var文件夹)'块,更新文件夹权限,从删除中排除有问题的文件夹或在尝试访问文件夹之前检查文件夹的权限。