views:

401

answers:

4

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?

+2  A: 

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 :)

krystan honour
The original question didn't indicate that any files were checked into the bin directory, only that they were copied there during the build. That being said, I agree that, in general, files should not be checked into the bin directory of a project.
CodeSavvyGeek
Ahh sorry about that, I've re-read it and you are right :)I did however find it very awkward that you are unable to use post and pre build events on these types of projects, in the end I ended up solving the problem by having a dependencies directory and msbuild always copies the assembly correctly.
krystan honour
A: 

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.

CodeSavvyGeek
+1  A: 

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.

Si
I am facing the very same problem. Your workaround works well but it's strange that TSVN acn delete the file, yet not overwrite it !?
Benjamin Baumann
+1  A: 

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

knst