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

发布于 2024-07-29 01:07:30 字数 406 浏览 4 评论 0原文

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

错误信息是:

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

看起来 Visual Studio 正在锁定该文件,但我不明白为什么需要这样做。 有没有办法防止 VS 锁定它已加载的项目中的 XML 文档文件?

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.

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."

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

预谋 2024-08-05 01:07:30

我找到了以下解决方案:
在 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 可在此处使用 http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

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

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

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

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

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

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

愿与i 2024-08-05 01:07:30

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

(感谢相关答案https://stackoverflow.com/a/8757941/251011

编辑:如果您也有问题对于出现此错误的dll文件,我最近发现了一个环境变量解决方案: https://stackoverflow.com/a/23069603/251011

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.

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

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

怎会甘心 2024-08-05 01:07:30

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

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

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.

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.

万人眼中万个我 2024-08-05 01:07:30

克里斯坦写道:

您可以将此文件放入另一个目录并从那里引用它,或者将使用它的代码放入库中,并让生成后事件将其复制到其 bin 目录,然后引用。

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

对于那些由于 TortoiseSVN->Update 而遇到此锁定问题的人,我目前正在尝试使用预更新挂钩,该挂钩会在更新之前删除有问题的文件(如果不需要更新,它们将被恢复),到目前为止这似乎有效(这很奇怪),但我还没有对其进行足够彻底的测试来确定。 如果证明可靠,将更新此答案。

希望 MS 在 VS 2010 中修复它。

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.

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.

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.

Here's hoping MS fix it in VS 2010.

池予 2024-08-05 01:07:30

基本上不要将文件签入 bin 文件夹,这是一个坏主意。

您可以将此文件放入另一个目录并从那里引用它,或者将使用它的代码放入库中,并让构建后事件将其复制到其 bin 目录,然后引用。

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

我们遇到了人们将内容签入 bin 目录的确切问题,除非您绝对必须,否则 bin 目录要么根本不签入,要么只包含 .refresh 文件在那里避免此类锁定问题。

回复晚了,抱歉:)

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

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.

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

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.

Bit late on the reply, sorry :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文