visual-studio 当我在 Visual Studio 中调试时,为什么会出现“文件被另一个进程使用”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1818076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get "file is used by another process" errors when I debug within Visual Studio?
提问by Chris Thompson
Using Visual Studio 2010 beta, when I run my application within the IDE for debugging, it works perfectly the first time. However, after closing the debug session, either by closing the application or clicking the stop debugging button, all subsequent attempts to debug the application fail with:
使用 Visual Studio 2010 测试版,当我在 IDE 中运行我的应用程序进行调试时,它第一次运行完美。但是,在关闭调试会话后,无论是通过关闭应用程序还是单击停止调试按钮,所有后续调试应用程序的尝试都会失败,并显示:
Error 1 Unable to copy file "obj\Debug\Application.dll" to "bin\Debug\Application.dll". The process cannot access the file 'bin\Debug\Application.dll' because it is being used by another process.
错误 1 无法将文件“obj\Debug\Application.dll”复制到“bin\Debug\Application.dll”。该进程无法访问文件“bin\Debug\Application.dll”,因为它正被另一个进程使用。
Handle.exe from SysInternals does show handles open, but even if I close the handles, the error doesn't go away. Any attempts to delete the file manually result in an "Access Denied" error message.
SysInternals 的 Handle.exe 确实显示句柄打开,但即使我关闭句柄,错误也不会消失。任何手动删除文件的尝试都会导致“拒绝访问”错误消息。
To fix this, I have to completely restart Visual Studio, afterwhich the Debug session will work once and stop again.
要解决此问题,我必须完全重新启动 Visual Studio,然后调试会话将工作一次并再次停止。
I'm not entirely sure when this started happening, but I'm pretty sure it's fairly recently.
我不完全确定这是什么时候开始发生的,但我很确定它是最近发生的。
UPDATE: After I force close the handles on Application.dll, I get the following error from VS:
更新:在我强制关闭 Application.dll 上的句柄后,我从 VS 收到以下错误:
Error 1 Unable to copy file "obj\Debug\Application.dll" to "bin\Debug\Application.dll". The requested operation cannot be performed on a file with a user-mapped section open.
错误 1 无法将文件“obj\Debug\Application.dll”复制到“bin\Debug\Application.dll”。无法对打开了用户映射部分的文件执行请求的操作。
What the heck is a "user-mapped section"??
“用户映射部分”到底是什么??
UPDATE 2: It appears that this problem occurs when I have a Form open in Design view when trying to debug. I'm going to do some more troubleshooting and then post my results.
更新 2:当我尝试调试时在设计视图中打开一个表单时,似乎会出现此问题。我将进行更多故障排除,然后发布我的结果。
UPDATE 3: I think I've narrowed it down to a form using a UserControl.
更新 3:我想我已经将它缩小到使用 UserControl 的表单。
采纳答案by Chris Thompson
To be honest with you, it sounds like a bug in VS2010. For some reason it isn't closing the open handles when the debugger stops. Killing the VS process automatically closes those handles, allowing you to access the file again. As a work around, you might look at unlockerit's free and works exceptionally well. I know that's not a great answer, but it should be faster than restarting VS. You might to consider sending a bug report too...
老实说,这听起来像是 VS2010 中的一个错误。出于某种原因,当调试器停止时,它不会关闭打开的句柄。终止 VS 进程会自动关闭这些句柄,从而允许您再次访问该文件。作为一种解决方法,您可能会查看解锁器,它是免费的并且运行良好。我知道这不是一个很好的答案,但它应该比重新启动 VS 更快。您也可以考虑发送错误报告...
Unlocker doesn't work on 64-bit OS, LockHunterdoes though.
Unlocker 不适用于 64 位操作系统,但LockHunter可以。
回答by samurab
Here is how I solved this problem
这是我解决这个问题的方法
*I open the project Properties, *select the build tab, *Clear the output path, *and buid(this will create the dll in the root folder) *come back to the output path and select browse(browse to the bin directory to either debug/release)and voila!
*我打开项目属性,*选择构建选项卡,*清除输出路径,*和buid(这将在根文件夹中创建dll)*回到输出路径并选择browse(浏览到bin目录以调试/发布)和瞧!
回答by warren.sentient
As per Error: Cannot access file bin/Debug/… because it is being used by another process answer by TarmoPikaro, sometimes Visual Studio creates multiple msbuild.exe ghost processes, which persist after build. These ghost processes seem to be causing file locks.
根据错误:无法访问文件 bin/Debug/... 因为它正被 TarmoPikaro 的另一个进程回答使用,有时 Visual Studio 会创建多个 msbuild.exe 幽灵进程,这些进程在构建后仍然存在。这些幽灵进程似乎导致文件锁定。
Solution 1 - Kill ghost MSBuild.exe's
解决方案 1 - 杀死 Ghost MSBuild.exe
Killing msbuild.exe's is a one time solution, it needs to be done per build basis.
杀死 msbuild.exe 是一次性解决方案,需要在每个构建基础上完成。
You can kill the processes as follows mrtumnus:
taskkill /f /im MSBuild.exe
taskkill /f /im MSBuild.exe
Solution 2 - Disable parallel builds in Visual Studio
解决方案 2 - 在 Visual Studio 中禁用并行构建
You can disable parallel build once and for all:
您可以一劳永逸地禁用并行构建:
Tools > Options > Projects and Solutions > Build and Run > "maximum numbers of parallel project builds" - by default it has value of 8, switch it to 1.
工具 > 选项 > 项目和解决方案 > 构建和运行 > “并行项目构建的最大数量” - 默认情况下它的值为 8,将其切换为 1。
Of course builds are bit slower now, but mileage may vary depending on your use case.
当然,构建现在有点慢,但里程可能会因您的用例而异。
This is related to Error: Cannot access file bin/Debug/... because it is being used by another process
回答by Andy West
I've seen the Windows Indexing Service cause this. Disabling it helped. Virus scanners can also be at fault. Mutliple Application.Close() calls can supposedly cause this, too.
我已经看到 Windows 索引服务导致了这个。禁用它有帮助。病毒扫描程序也可能有问题。多个 Application.Close() 调用也可能导致这种情况。
Of course, since it always works the first time, I suppose these are unlikely.
当然,因为它总是第一次起作用,我想这些不太可能。
回答by Drishti
Had the same problem. The following things helped
有同样的问题。以下事情有所帮助
- Closing all design files while debugging
- using unlocker
- 调试时关闭所有设计文件
- 使用解锁器
Also my application opens a port. While debugging an exception was thrown and program quit. While ending the program I closed the port. That helped too.
我的应用程序也打开了一个端口。调试时抛出异常并退出程序。在结束程序时,我关闭了端口。这也有帮助。
But definitely, bug with VS2010.
但绝对是 VS2010 的错误。
回答by Juha Palom?ki
I encountered the same problem and in my case I had the file in question open in Visual Studio. Closing all files helped.
我遇到了同样的问题,就我而言,我在 Visual Studio 中打开了有问题的文件。关闭所有文件有帮助。
回答by Lali
I faced the same error and I was stuck in it for many days. Finally resolved the issue. I was working on a project that had many class libraries added in it. I added the reference of these libraries to my main project and mistakenly added reference to same project to itself. So when I removed self reference, it worked.
我遇到了同样的错误,并且被困在其中很多天。终于解决了这个问题。我正在处理一个添加了许多类库的项目。我将这些库的引用添加到我的主项目中,并错误地将同一项目的引用添加到了自身。所以当我删除自我引用时,它起作用了。
回答by raging_river
I had this issue myself. I had the project properties window open and that apparently creates a file lock. Even after I closed the window the file lock remained and I had to restart VS.
我自己也有这个问题。我打开了项目属性窗口,这显然会创建一个文件锁。即使在我关闭窗口后,文件锁仍然存在,我不得不重新启动 VS。
P.S. I'm using VS 2019. Just posting this for anyone having the issue I had and coming to this post.
PS 我正在使用VS 2019。只是为遇到我遇到的问题并来到此帖子的任何人发布此信息。

