我在c#.net中構建了一個Windows服務。我添加了Pre-Build和Post-Build事件以自動在構建上部署我的服務。但有時我得到這個錯誤:無法構建我的服務:無法複製文件,因爲它正在被另一個進程使用
無法複製文件 「[CompletPath ...] \斌\調試\ Business.Data.dll」 到 的 「bin \調試\ Business.Data.dll」。該進程無法訪問文件 'bin \ Debug \ Business.Data.dll',因爲它正被另一個 進程使用。
在Pre-Build事件中,我正在關閉該服務,從而終止使用Debug目錄中的文件並卸載該服務的所有任務。有一個在蝙蝠的代碼,我在預生成事件運行:
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
在我安裝和啓動服務生成後事件,有代碼,即使這是不是這個問題,因爲我在構建中gettring錯誤,所以post-build事件沒有執行。
SET serviceName=%1
SET frameworkPath=%2
SET targetServicePath=%3
%frameworkPath%\installutil.exe /ShowCallStack %targetServicePath%
net start %serviceName%
我並不總是有問題。我第一次建設時通常會遇到問題,我正在清理解決方案,再次構建,並且通常在此之後工作。
您是否嘗試過使用某些軟件來找出鎖定文件的內容?它是Visual Studio還是您的服務?如果沒有,請嘗試使用: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx – Niclas 2013-04-29 20:10:39
我正在使用handle.exe殺死使用文件夾中文件的每個進程... – forumma 2013-05-01 14:32:10