【发布时间】:2013-04-26 13:31:46
【问题描述】:
我在 c# .net 中构建了一个 Windows 服务。我添加了 Pre-Build 和 Post-Build 事件以在构建上自动部署我的服务。但有时我得到了这个错误:
无法将文件“[CompletPath...]\bin\Debug\Business.Data.dll”复制到 “bin\Debug\Business.Data.dll”。进程无法访问文件 'bin\Debug\Business.Data.dll' 因为它正在被另一个人使用 过程。
在 Pre-Build 事件中,我将关闭服务,杀死所有使用 Debug 目录中文件的任务并卸载服务。我在 Pre-Build 事件中运行的 .bat 中有代码:
SET executionPath=%~dp0
SET serviceName=%1
SET frameworkPath=%2
SET targetServicePath=%3
SET targetBinPath=%~4
set targetBinPath=%targetBinPath:~0,-2%
net stop %serviceName%
powershell -NonInteractive -executionpolicy Unrestricted -file "%executionPath%\unlockfiles.ps1" "%targetBinPath%"
%frameworkPath%\installutil.exe /u %targetServicePath%
Exit /b 0
在我安装和启动服务的构建后事件中,即使这不是问题,也有代码,因为我在构建时遇到错误,所以构建后事件没有执行。
SET serviceName=%1
SET frameworkPath=%2
SET targetServicePath=%3
%frameworkPath%\installutil.exe /ShowCallStack %targetServicePath%
net start %serviceName%
我并不总是遇到问题。我通常在第一次构建时遇到问题,我正在清理解决方案,再次构建并且通常在此之后它可以工作。
【问题讨论】:
-
您是否尝试过使用一些软件来找出锁定文件的原因?是 Visual Studio 还是您的服务?如果没有,请尝试使用:technet.microsoft.com/en-us/sysinternals/bb896653.aspx
-
我正在使用 handle.exe 来杀死使用文件夹中文件的每个进程...
标签: c# visual-studio-2010 windows-services filelock pre-build-event