Visual Studio 2010构建文件锁定问题

God*_*eke 21 visual-studio-2010

我有一个Visual Studio 2008项目,我"升级"到Visual Studio 2010.自升级以来,我一直遇到很多项目问题(一个项目过去和现在仍然是警察,我可能会添加).

第一个问题是构建主可执行文件会锁定可执行文件,从而导致进一步的重建失败.这是在一个相关的问题中描述的:Visual Studio在构建锁定输出文件,我选择了解决方法:

if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if exist "$(TargetPath)" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
Run Code Online (Sandbox Code Playgroud)

除了这个解决方法只适用一次.然后.locked文件也被devenv.exe锁定,必须移动.我一直在通过添加.1.locked,.2.locked等来解决这个问题.唯一一次删除锁以便删除文件是关闭devenv.exe(UI后需要几秒钟)消失,然后可以删除文件).

调试器不必用于导致此问题的事实表明2010构建系统存在相当严重的问题.

我认为我可以打折的一些理论:

  • 防病毒或其他后台任务:如果这是一个问题,它似乎会失败.然而,作为一个完整的我删除了avast!系统完全没有运气.

更新:此项目在没有防病毒且没有备份实用程序的计算机上具有相同的症状.办公室的机器运行XP SP3 32bit,我的本地机器是Windows 7 64位.这似乎与操作系统无关.

  • 调试器正在锁定文件:重现此操作所需的全部内容是重复构建过程而不进行调试.ProcessExplorer显示devenv.exe是锁的持有者,而不是vshost并且杀死vshost.exe无论如何都不会删除锁.

我有一个次要问题,一旦文件被锁定就开始出现:表单设计者停止加载"无法找到程序集"错误.我怀疑这些与早期的锁定问题有关,因为设计人员构建之前就开始了,但是进行任何更改和重建都会导致所有设计人员崩溃并出现错误(即使是我已经打开并作为当前视图).

观看靠近白色错误屏幕的表单是可怜的,因为你将"dummy = 1"改为"dummy = 2",其中"dummy"绝对没有任何东西,只是强制重新编译一个完全不相关的程序集.

更新:我已经尝试了一些补救措施:未选中启用.NET源步进,因此这不是问题.删除.SUO(解决方案用户选项)只需重启通常可以解决问题(两个构建:第一个因为没有锁定文件而第二个因为有一个,但它可以由脚本重命名).

Error   28  Unable to copy file "obj\Debug\PolicyTracker3.exe" to "bin\Debug\PolicyTracker3.exe". 
The process cannot access the file 'bin\Debug\PolicyTracker3.exe' because it is being used by another process.  
Run Code Online (Sandbox Code Playgroud)

God*_*eke 15

在修补程序为此之前,我有以下解决方法.只需使用类似的东西调用"C:\MyBin\VisualStudioLockWorkaround.exe" "$(TargetPath)"(将MyBin替换为放置可执行文件的位置).

将其创建为C#控制台应用程序,并以与原始预构建重命名相同的方式在预构建部分中使用(有关详细信息,请参阅原始问题的顶部).

 using System;
 using System.IO;

 namespace VisualStudioLockWorkaround
 {
  class Program
  {
   static void Main(string[] args)
   {
    string file = args[0];
    string fileName = Path.GetFileName(file);
    string directory = Path.GetDirectoryName(args[0]);
    if (!Directory.Exists(directory)) //If we don't have a folder, nothing to do.
    {
     Console.WriteLine(String.Format("Folder {0} not found. Exiting.", directory));
     return;
    }
    if (!File.Exists(file)) //If the offending executable is missing, no reason to remove the locked files.
    {
     Console.WriteLine(String.Format("File {0} not found. Exiting.", file));
     return;
    }
    foreach (string lockedFile in Directory.EnumerateFiles(directory, "*.locked"))
    {
     try //We know IOExceptions will occur due to the locking bug.
     {
      File.Delete(lockedFile);
     }
     catch (IOException)
     {
      //Nothing to do, just absorbing the IO error.
     }
     catch (UnauthorizedAccessException)
     {
      //Nothing to do, just absorbing the IO error.
     }                                        
    }

    //Rename the executable to a .locked
    File.Move(file, Path.Combine(directory, String.Format("{0}{1:ddmmyyhhmmss}.locked", fileName, DateTime.Now)));
   }
  }
 }
Run Code Online (Sandbox Code Playgroud)