Link to home
Start Free TrialLog in
Avatar of chrisfixit
chrisfixit

asked on

How can I delete a 'locked' file in VB NET?

I have a windows service that I wrote in VB NET which periodically copies and replaces a file in another applications directory.

This has been working fine, but the latest version of the app now has the file I wish to overwrite 'locked' so my service fails when I try and delete the file.

How can I get around this - it won't cause a crash cos the program I'm overwriting only shows a splash screen which I overwrite with my own depending on the time of day.
Avatar of xtravagan
xtravagan

How does your program overwrite it? Normally you can copy data into an existing file, but not remove and add a file that is locked. However that all depends on the share flags used by the application owning the file.

Normally a locked file is locked, that's the whole idea and you can't get around it without shutting down the application that holds the lock (well there are ways but they are advanced and danagerous).
Avatar of chrisfixit

ASKER

advanced and dangerous is good.

At the moment I simply..

If System.IO.File.Exists(NewCopy) = True Then
                System.IO.File.Delete(NewCopy)
            End If
            If System.IO.File.Exists(FileToCopy) = True Then
                System.IO.File.Copy(FileToCopy, NewCopy)
            End If
ASKER CERTIFIED SOLUTION
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It seems it is a different problem..for some reason on that particular system the whole directory is write protected - i can't see what is doing it .

Anyway thanks.
Even if it is closed, I would like to propose using sysinternals process explorer and process monitor to nail these things. In Process Monitor you can search for handles by name of the file they point to. So in this case you can look for your directory in that search and see who holds it.

Also with procmon running you can watch your own process to see exactly what windows calls are being made and what the system results are to better understand the problem.

http://live.sysinternals.com/