views:

201

answers:

1

Related: How to force delete a file?

I have a NAnt script that does a full build and deployment to our Dev environment. Deploying the C#.Net Win exe and its used DLLs involves moving them to a network location, where our testers and other developers run them.

    <copy todir="${dest.dir}" overwrite="true" failonerror="false" >
        <fileset basedir="${source.dir}" >
            <include name="**/*" />
        </fileset>
    </copy>

Right now, the script calls the Copy task, with Overwrite="true", but this fails if anyone is running the application, reporting

Cannot copy 'source.dll' to 'dest.dll'. Access to the path 'dest.dll' is denied.

"dest.dll" is one of the main EXE's dependencies, copied along with it. Right now, I have one of two recourses: I either figure out who has it open and ask them to quit, or I send an email to our systems engineers and they do some voodoo to delete the locked file. Is there anyway I can incorporate some of my own voodoo into the NAnt script, so the file copy always succeeds?

+1  A: 

You have a few possible options.

One would be to write a custom NAnt task that uses P/Invoke to call either the UnlockFile or UnlockFileEx methods from the Win32 API. This task could wrap the unlock and copy operations so you would only need to call your new task from the NAnt script.

Another would be to use the NAnt exec task and execute a command line unlock utlity before trying to copy.

Scott Dorman
The problem with that, is that only works for processes on the local machine. I don't think UnlockFile would be able to unlock the file from another user on another box.
Dov
You may want to look at some of the utilities from SysInternals - http://technet.microsoft.com/en-us/sysinternals/default.aspx to see if there are any command line utilities you can use that will work on remote computers.
Scott Dorman
It seems like one of those utilities might have been the only way to do it, but I basically gave up.
Dov