如何防止Visual Studio锁定bin目录中的xml文档文件?

时间:2023-01-14 19:07:08

My visual studio solution includes a web application and a unit test application. My web application uses log4net. I want to be able to use msbuild from the command-line to build my solution. However, whenever I build the solution from the command-line, I get build errors because it can't copy log4net.xml to the test project's bin directory.

我的visual studio解决方案包括Web应用程序和单元测试应用程序。我的Web应用程序使用log4net。我希望能够从命令行使用msbuild来构建我的解决方案。但是,每当我从命令行构建解决方案时,我都会遇到构建错误,因为它无法将log4net.xml复制到测试项目的bin目录中。

The error message is:

错误消息是:

"Unable to copy file '\bin\log4net.xml' to 'bin\Debug\log4net.xml'. Access to the path '\bin\log4net.xml' is denied."

“无法将文件'\ bin \ log4net.xml'复制到'bin \ Debug \ log4net.xml'。拒绝访问路径'\ bin \ log4net.xml'。”

It looks like Visual Studio is locking this file, but I can't figure out why it would need to. Is there a way to prevent VS from locking the XML documentation files in a project that it has loaded?

看起来Visual Studio正在锁定此文件,但我无法弄清楚它为什么需要。有没有办法阻止VS在已加载的项目中锁定XML文档文件?

5 个解决方案

#1


I've had this problem with Visual Studio, too. We use NAnt instead of MSBuild, but the problem is the same. I was able to work around it by modifying the build file to ignore failures when copying xml documentation.

我也遇到过Visual Studio这个问题。我们使用NAnt而不是MSBuild,但问题是一样的。我能够通过修改构建文件来解决它,以便在复制xml文档时忽略失败。

Note that this doesn't actually solve the original problem since the xml files are still locked, but this workaround was good enough for us since the actual content of our xml documentation doesn't change very often.

请注意,这实际上并没有解决原始问题,因为xml文件仍然被锁定,但是这种解决方法对我们来说已经足够了,因为我们的xml文档的实际内容不会经常更改。

#2


I've found the following solution: In VS postbuild event or in NAnt/MSbuild script execute the cmd script

我找到了以下解决方案:在VS postbuild事件或NAnt / MSbuild脚本中执行cmd脚本

handle.exe -p devenv [Path to the folder with locked files] > handles.txt

FOR /F "skip=5 tokens=3,4 delims=: " %%i IN (handles.txt) DO handle -p %%i -c %%j -y

handle.exe is available here http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

handle.exe可在此处获得http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

first line of the script dumps to handles.txt all handles for files locked by VS second line reads handle ids from the file and kills the handles

脚本的第一行转储到handles.txt由VS第二行锁定的文件的所有句柄从文件中读取句柄ID并终止句柄

After the script is executed files may be removed/replaced/moved etc

脚本执行后,可以删除/替换/移动文件等

#3


If you're fine with omitting the xml & pdb files altogether from the output, you can pass /p:AllowedReferenceRelatedFileExtensions=none to msbuild on the command line.

如果你可以从输出中完全省略xml和pdb文件,你可以在命令行上将/ p:AllowedReferenceRelatedFileExtensions = none传递给msbuild。

(Thanks to related answer https://*.com/a/8757941/251011 )

(感谢相关回答https://*.com/a/8757941/251011)

EDIT: If you also have problems with dll files having this error, I recently discovered an environment variable solution: https://*.com/a/23069603/251011

编辑:如果您也遇到此错误的DLL文件问题,我最近发现了一个环境变量解决方案:https://*.com/a/23069603/251011

#4


Krystan wrote:

You could drop this file into another directory and reference it from there or place code that uses it into a library and have the post build event on that copy it to its bin directory and then reference.

您可以将此文件放入另一个目录并从那里引用它,或将使用它的代码放入库中,并在该副本上将post build事件复制到其bin目录然后引用。

Our xml file locking problem is not in the projects bin directory, rather an external reference directory. We hit it when performing TortoiseSVN->Update where a new version is available. Assuming it's because VS is using the file for intellisense.

我们的xml文件锁定问题不在项目bin目录中,而是在外部引用目录中。我们在执行TortoiseSVN-> Update时遇到了新版本。假设这是因为VS正在使用该文件进行智能感知。

For those who hit this locking issue due to TortoiseSVN->Update, I'm currently experimenting with a pre-update hook which deletes the offending file(s) before updating (they will be restored if no update is needed), so far this seems to work (which is weird) but I haven't tested it thoroughly enough to say for sure. Will update this answer if it proves reliable.

对于因TortoiseSVN-> Update而遇到此锁定问题的人,我目前正在尝试使用更新前的挂钩,在更新之前删除有问题的文件(如果不需要更新,它们将被恢复),到目前为止似乎工作(这很奇怪),但我没有彻底测试它,以确定。如果证明可靠,将更新此答案。

Here's hoping MS fix it in VS 2010.

这里希望MS在VS 2010中修复它。

#5


Basically don't check files into the bin folder, its a bad idea.

基本上不检查文件到bin文件夹,这是个坏主意。

You could drop this file into another directory and reference it from there or place code that uses it into a library and have the post build event on that copy it to its bin directory and then reference.

您可以将此文件放入另一个目录并从那里引用它,或将使用它的代码放入库中,并在该副本上将post build事件复制到其bin目录然后引用。

Msbuild will then copy that to the webprojects bin directory for you :)

然后,Msbuild会将它复制到webprojects bin目录中:)

We have this exact issue with people checking in stuff to the bin directory, unless you absolutely have to bin directories should either not be checked in at all or just have .refresh files in there to avoid these sorts of locking issues.

我们有这个确切的问题,人们检查东西到bin目录,除非你绝对必须bin目录应该根本不检查或只是在那里有.refresh文件,以避免这些类型的锁定问题。

Bit late on the reply, sorry :)

回复迟到了,抱歉:)

#1


I've had this problem with Visual Studio, too. We use NAnt instead of MSBuild, but the problem is the same. I was able to work around it by modifying the build file to ignore failures when copying xml documentation.

我也遇到过Visual Studio这个问题。我们使用NAnt而不是MSBuild,但问题是一样的。我能够通过修改构建文件来解决它,以便在复制xml文档时忽略失败。

Note that this doesn't actually solve the original problem since the xml files are still locked, but this workaround was good enough for us since the actual content of our xml documentation doesn't change very often.

请注意,这实际上并没有解决原始问题,因为xml文件仍然被锁定,但是这种解决方法对我们来说已经足够了,因为我们的xml文档的实际内容不会经常更改。

#2


I've found the following solution: In VS postbuild event or in NAnt/MSbuild script execute the cmd script

我找到了以下解决方案:在VS postbuild事件或NAnt / MSbuild脚本中执行cmd脚本

handle.exe -p devenv [Path to the folder with locked files] > handles.txt

FOR /F "skip=5 tokens=3,4 delims=: " %%i IN (handles.txt) DO handle -p %%i -c %%j -y

handle.exe is available here http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

handle.exe可在此处获得http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

first line of the script dumps to handles.txt all handles for files locked by VS second line reads handle ids from the file and kills the handles

脚本的第一行转储到handles.txt由VS第二行锁定的文件的所有句柄从文件中读取句柄ID并终止句柄

After the script is executed files may be removed/replaced/moved etc

脚本执行后,可以删除/替换/移动文件等

#3


If you're fine with omitting the xml & pdb files altogether from the output, you can pass /p:AllowedReferenceRelatedFileExtensions=none to msbuild on the command line.

如果你可以从输出中完全省略xml和pdb文件,你可以在命令行上将/ p:AllowedReferenceRelatedFileExtensions = none传递给msbuild。

(Thanks to related answer https://*.com/a/8757941/251011 )

(感谢相关回答https://*.com/a/8757941/251011)

EDIT: If you also have problems with dll files having this error, I recently discovered an environment variable solution: https://*.com/a/23069603/251011

编辑:如果您也遇到此错误的DLL文件问题,我最近发现了一个环境变量解决方案:https://*.com/a/23069603/251011

#4


Krystan wrote:

You could drop this file into another directory and reference it from there or place code that uses it into a library and have the post build event on that copy it to its bin directory and then reference.

您可以将此文件放入另一个目录并从那里引用它,或将使用它的代码放入库中,并在该副本上将post build事件复制到其bin目录然后引用。

Our xml file locking problem is not in the projects bin directory, rather an external reference directory. We hit it when performing TortoiseSVN->Update where a new version is available. Assuming it's because VS is using the file for intellisense.

我们的xml文件锁定问题不在项目bin目录中,而是在外部引用目录中。我们在执行TortoiseSVN-> Update时遇到了新版本。假设这是因为VS正在使用该文件进行智能感知。

For those who hit this locking issue due to TortoiseSVN->Update, I'm currently experimenting with a pre-update hook which deletes the offending file(s) before updating (they will be restored if no update is needed), so far this seems to work (which is weird) but I haven't tested it thoroughly enough to say for sure. Will update this answer if it proves reliable.

对于因TortoiseSVN-> Update而遇到此锁定问题的人,我目前正在尝试使用更新前的挂钩,在更新之前删除有问题的文件(如果不需要更新,它们将被恢复),到目前为止似乎工作(这很奇怪),但我没有彻底测试它,以确定。如果证明可靠,将更新此答案。

Here's hoping MS fix it in VS 2010.

这里希望MS在VS 2010中修复它。

#5


Basically don't check files into the bin folder, its a bad idea.

基本上不检查文件到bin文件夹,这是个坏主意。

You could drop this file into another directory and reference it from there or place code that uses it into a library and have the post build event on that copy it to its bin directory and then reference.

您可以将此文件放入另一个目录并从那里引用它,或将使用它的代码放入库中,并在该副本上将post build事件复制到其bin目录然后引用。

Msbuild will then copy that to the webprojects bin directory for you :)

然后,Msbuild会将它复制到webprojects bin目录中:)

We have this exact issue with people checking in stuff to the bin directory, unless you absolutely have to bin directories should either not be checked in at all or just have .refresh files in there to avoid these sorts of locking issues.

我们有这个确切的问题,人们检查东西到bin目录,除非你绝对必须bin目录应该根本不检查或只是在那里有.refresh文件,以避免这些类型的锁定问题。

Bit late on the reply, sorry :)

回复迟到了,抱歉:)