我发现自己处于令人讨厌的情况,即在调试会话完成后,visual studio调试器不能自行释放。因此,devenv.exe的保留对可执行文件的锁,我不能重建项目,由于可怕的错误:以后每运行周期是不强制Visual Studio 2008调试器发布可执行文件
Error 1 Unable to copy file "obj\Debug\program.exe" to "bin\Debug\program.exe". The process cannot access the file 'bin\Debug\program.exe' because it is being used by another process.
这可以通过重新启动Visual Studio中,但重新启动我的整个IDE固定恰好有利于良好的编码环境。建立 - >清洁没有帮助。
我google了这个错误,虽然症状似乎是相当普遍的基本原因是不同的。主要我想知道,按重要性排序:
- 是否有任何快速的方法来解锁文件,而无需重新启动Visual Studio?
- 除此之外,我应该采用什么防御性编程技术来防止这种情况?
- 在下面的例子中幕后究竟发生了什么,导致调试器不能释放?
一个会产生这种症状的代码的例子就在这里。
class Program
{
static void Main(string[] args)
{
var f1 = new Form1();
f1.Dir = "asdf";
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FileSystemWatcher fsw;
public string Dir
{
get { return fsw.Path;}
set
{
fsw = new FileSystemWatcher(value);
fsw.EnableRaisingEvents = true;
throw new Exception("asdf");
}
}
~Form1()
{
if (fsw != null)
fsw.Dispose();
}
}
重现:
- 运行Program.Main使用Visual Studio 2008中的调试器。
- 当抛出异常时停止调试。
- 更改源代码并尝试重建。
编辑:各种各样的解决方案:
public Form1()
{
InitializeComponent();
this.Closing += (sender, args) =>
{
if (watcher != null)
watcher.Dispose();
};
}
我仍然有兴趣在为什么这个工作,并同时将其放置在析构函数没有。
您使用的是Windows 7吗? – 2009-12-24 16:11:43
没有。带有VS 9.0.30729.1 SP的Windows XP SP3。这是为.NET 3.5 SP1构建的项目 – fostandy 2009-12-24 16:23:53