简体   繁体   English

在Visual Studio 2010中生成时锁定文件

[英]Locking files when building in Visual Studio 2010

Hello there, Stackoverflow. 你好,Stackoverflow。

Recently, when I've been programming in Visual Studio 2010, I've been getting the problem with VS locking the bin/Debug/(ProjectName).exe file when trying to build and gives me the error below after trying to build the project 10 times: 最近,当我在Visual Studio 2010中进行编程时,我遇到了VS在尝试构建时锁定bin / Debug /(ProjectName).exe文件的问题,并在尝试构建项目后给出了以下错误10次​​:

Unable to copy file "obj\\x86\\Debug\\TileEngine.exe" to "bin\\x86\\Debug\\TileEngine.exe". 无法将文件“obj \\ x86 \\ Debug \\ TileEngine.exe”复制到“bin \\ x86 \\ Debug \\ TileEngine.exe”。 The process cannot access the file 'bin\\x86\\Debug\\TileEngine.exe' becuase it is being used by another process. 该进程无法访问文件'bin \\ x86 \\ Debug \\ TileEngine.exe',因为它正由另一个进程使用。

The problem appears when I edit the source and then try to Debug. 当我编辑源然后尝试调试时出现问题。 I've checked using different programs, and the only program using the file is Visual Studio. 我已经检查过使用不同的程序,使用该文件的唯一程序是Visual Studio。

If I wait for about 10 minutes before trying to build, it seems to work properly, but when trying different things, it isn't good needing to wait 10 minutes before trying something. 如果我在尝试构建之前等待大约10分钟,它似乎工作正常,但是当尝试不同的事情时,在尝试某些事情之前需要等待10分钟并不好。

I've tried different solutions both on this site as well as everywhere I can find on Google. 我在这个网站上以及我在Google上找到的所有地方都尝试了不同的解决方案。

Some solutions I've found, but haven't worked for me 我找到的一些解决方案,但没有为我工作

Solution 1 - Using a pre-build script 解决方案1 ​​ - 使用预构建脚本

In some different questions here on Stackoverflow, I've found one solution being that you go into Project Properties > Build Events and then in the Pre-build event command line add: 在Stackoverflow的一些不同的问题中,我发现一个解决方案是你进入Project Properties > Build Events ,然后在预构建事件命令行中添加:

if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"

This made it possible for me to build the project one more time than I usually could, but when editing the code again, and then building, the same error appeared. 这使我有可能比平时更多地构建项目,但是当再次编辑代码然后构建时,出现了同样的错误。

Note: Trying to build a release instead of a debug build seems to break the pre-build script and it exits with the code '1', which seems to make VS unable to build properly. 注意: 尝试构建版本而不是调试版本似乎打破了预构建脚本,并且它以代码“1”退出,这似乎使VS无法正确构建。 Removing the pre-build script makes it work like "normal" again, still with the same error though. 删除预构建脚本使其再次像“正常”一样工作,但仍然有相同的错误。

Solution 2 - Running Visual Studio as Administrator 解决方案2 - 以管理员身份运行Visual Studio

This is another solution I've found, but havent worked either for me, so I assume that Visual Studio already have all the permissions required and running as Administrator doesn't actually make any difference. 这是我发现的另一个解决方案,但是对我来说也没有用,所以我认为Visual Studio已经拥有所需的所有权限并且以管理员身份运行实际上没有任何区别。

Solution 3 - Changing the AssemblyVersion 解决方案3 - 更改AssemblyVersion

In this question, Visual Studio build fails: unable to copy exe-file from obj\\debug to bin\\debug , I found another solution that included changing the AssemblyVersion , in the Properties\\AssemblyInfo.cs file, to "2.0.0.0" . 在这个问题上, Visual Studio中生成失败:无法从OBJ \\调试EXE文件复制到BIN \\调试 ,我发现另一种解决方案,其中包括改变AssemblyVersion ,在Properties\\AssemblyInfo.cs的文件,到"2.0.0.0" This, however, haven't made any difference whatsoever for me. 然而,这对我没有任何影响。

Solution 4 - Closing UserControl designers before building 解决方案4 - 在构建之前关闭UserControl设计器

According to some different answers here and there on the Internet, Visual Studio apparently uses the built project executable to render the UserControl designer(?). 根据Internet上的不同答案,Visual Studio显然使用构建的项目可执行文件来呈现UserControl设计器(?)。 In my case, this is probably not it, though, since I use XNA mostly and it doesn't use the UserControl designer. 在我的情况下,这可能不是它,因为我主要使用XNA而且它不使用UserControl设计器。

Solution 5 - Cleaning up resources when application quits 解决方案5 - 在应用程序退出时清理资源

This might be a solution that I have failed to implement properly. 这可能是我未能正确实施的解决方案。 I'm just thinking though, that if this is the solution, how come I haven't been required to do it before. 我只是想,如果这是解决方案,我怎么没有被要求这样做。 I assume XNA unloads everything that gets loaded through the Content pipeline, therefore this solution wouldn't' make any real sense. 我假设XNA卸载了通过Content管道加载的所有Content ,因此这个解决方案不会“真正有意义”。

If there is anyone that is able to spread some light on this issue, it would be really awesome, as it is stopping me from programming anything really, because I don't like waiting for 10 minutes because I've made a 2 second change all the time. 如果有人能够在这个问题上传播一些亮点,那真的很棒,因为它阻止我编程任何东西,因为我不喜欢等待10分钟因为我做了2秒钟的改变每时每刻。

I've run into this problem a few times myself. 我自己曾经遇到过这个问题几次。

Mine might not be from the same cause as yours, but I'll tell you what went wrong with me and how I fixed it, hopefully it'll be helpful to you. 我可能与你的原因不一样,但我会告诉你我出了什么问题,以及我如何解决它,希望它对你有所帮助。

Basically, my program never fully exited properly, even when it appeared to. 基本上,我的程序从未完全退出,即使它似乎。 It would continue to run, and thus continue to lock down the file. 它将继续运行,从而继续锁定文件。

A quick dirty fix I used initially (and a way to prove if this is the case) is: 我最初使用的快速脏修复(以及证明是否是这种情况的方法)是:

  • Open Task Manager (Ctrl-Alt-Del) 打开任务管理器(Ctrl-Alt-Del)
  • Click Processes tab 单击“进程”选项卡
  • Look for your program's name (TileEngine.exe) 查找程序的名称(TileEngine.exe)
  • Note: There will probably be name_vshost.exe (TileEngine_vshost.exe) That's a VisualStudio thing, ignore that, it's not relevant. 注意:可能会有name_vshost.exe(TileEngine_vshost.exe)这是一个VisualStudio的东西,忽略它,它是不相关的。
  • If you find it, it means your program hasn't actual exited fully. 如果你找到它,这意味着你的程序没有完全退出。
  • If it's there, click on it and press "End Process" 如果它在那里,单击它并按“结束进程”

So if it's there, then for some reason, your program didn't shut down, like mine did. 所以,如果它存在,那么由于某种原因,你的程序没有像我的那样关闭。

Often, this is from a thread being launched and forgotten, or an Async task that never completes, or something like that. 通常,这是来自正在启动和忘记的线程,或者从未完成的异步任务,或类似的东西。

Make sure in your OnExiting(..) void function that you kill all running threads. 确保在OnExiting(..)void函数中杀死所有正在运行的线程。

If your program is still running despite best attempts to close all threads and other blockers, you can use the very dirty bad method: In OnExiting(...) run the code "System.Diagnostics.Process.GetCurrentProcess().Kill();" 如果你的程序仍在运行,尽管最好尝试关闭所有线程和其他阻塞程序,你可以使用非常脏的方法:在OnExiting(...)中运行代码“System.Diagnostics.Process.GetCurrentProcess()。Kill() ;” - this will taskmanager-style forceshutdown the current process... this is only as an emergency I-can't-make-it-work-any-other-way method. - 这将是taskmanager-style forceshutdown当前进程...这只是一个紧急情况,我不能让它工作任何其他方法。

I think I found the solution myself. 我想我自己找到了解决方案。 In the Project Properties, "Enable the Visual Studio hosting process" wasn't checked. 在项目属性中,未选中“启用Visual Studio主机进程”。 Checking it seems to have fixed the problems, at least for now. 检查它似乎已经解决了问题,至少目前是这样。

Got reminded of it from mcmonkey4eva's post. 从mcmonkey4eva的帖子中提醒它。 So thanks for that =) 所以,谢谢你=)

And thanks for the other replied I've got. 谢谢对方回复我的意见。 Stackoverflow is awesome! Stackoverflow太棒了!

Have you checked if any files are being blocked by your firewall? 您是否检查过防火墙是否阻止了任何文件? When I switched to the full version of Avast I find I have to disable the File System Shield It loves to remove my executable files when I try to run my visual studio projects. 当我切换到Avast的完整版本时,我发现我必须禁用File System Shield当我尝试运行我的visual studio项目时,它喜欢删除我的可执行文件。

I had problems when upgrading to VS2012 Professional. 升级到VS2012 Professional时遇到问题。 (SDK, .Net, Visual C++ Redistributable package) (SDK,.Net,Visual C ++ Redistributable包)

ENSURE ALL OF THESE ARE COMPATIBLE WITH THE CURRENT VERSION OF VS YOU ARE USING 确保所有这些都与您使用的VS的当前版本兼容

What I did, was I ended up uninstalling EVERYTHING that was associated with both Visual Studio downloads. 我做了什么,是我最终卸载了与Visual Studio下载相关的一切。 If you are able to remove and save your project files elsewhere and then bring them back. 如果您能够在其他地方删除并保存项目文件,然后将其恢复。 Go through all your program files to see if there is anything hidden in the wrong folder and check your C drive. 浏览所有程序文件以查看是否有任何隐藏在错误文件夹中的内容并检查您的C盘。

Which meant downloading and reinstalling (fresh): 这意味着下载并重新安装(新鲜):

I think if you clean out your program files, it should be ok. 我想如果你清理你的程序文件,它应该没问题。 I wouldn't recommend going into your register unless you are very sure of what you are doing. 除非你非常确定你在做什么,否则我不建议你进入你的注册。 IF you have already made changes to the register then we'll have a look at that and other options (if this doesn't solve your problem). 如果您已经对寄存器进行了更改,那么我们将查看该选项和其他选项(如果这不能解决您的问题)。

尝试通过取消选中文件夹级别从解决方案中删除只读检查。

I ran into this problem and in my case was due to having bin included in the solution; 我遇到了这个问题,在我的情况下是由于在解决方案中包含了bin; as soon as I excluded the bin folder from my solution the problem went away. 一旦我从我的解决方案中排除了bin文件夹,问题就消失了。

没有任何帮助,没有预装命令,也没有设计师关闭,但我找到了一种帮助我的方法,只需从调试更改为发布,反之亦然,释放锁定文件,您可以在不关闭IDE的情况下删除它们。

I regularly get this problem if I switch from Debug to Release and then immediately F5 to compile. 如果我从Debug切换到Release然后立即F5进行编译,我经常遇到这个问题。 Crazy as it sounds, waiting for, say, one minute after switching between modes will prevent this. 听起来很疯狂,等待,比如说,在模式之间切换一分钟就可以防止这种情况发生。

If it's locked, the only solution is to close Visual Studio and re-open. 如果它被锁定,唯一的解决方案是关闭Visual Studio并重新打开。

I solved this problem organizing my resources on solution. 我解决了这个问题,组织我的解决方案资源。 I noticed this error when I put some images on my application at the same solution folder. 当我在同一解决方案文件夹中的应用程序上放置一些图像时,我注意到了这个错误

So, 所以,

  • I'd removed all images and resources from app, save without it. 我从应用程序中删除了所有图像和资源,没有它就保存。
  • Moved the images to outside Solution folder. 将图像移动到解决方案文件夹外部。
  • Open the solution and add this images again, using "Import" button at controls. 打开解决方案并使用控件上的“导入”按钮再次添加此图像。

If you will try this, remember to do with Application Icon, on Project Settings. 如果您将尝试此操作,请记住在“项目设置”中使用“应用程序图标”。

Now, everything is working fine for me. 现在,一切都对我很好。

I hope it helps. 我希望它有所帮助。

You need to disable Windows Indexer as it locks up the file 您需要在锁定文件时禁用Windows Indexer

Follow this Guide how to disable 请遵循本指南如何禁用

In my case the problem seem to be caused by the remote debugger. 在我的情况下,问题似乎是由远程调试器引起的。 It starts on local machine when you compile with "x64" option. 当您使用“x64”选项进行编译时,它将在本地计算机上启动。 Try to change the project setting (properties/buid) until you reach the final version. 尝试更改项目设置(properties / buid),直到达到最终版本。

将构建平台目标从x86更改为任何CPU。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM