Solved

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

Posted on 2009-04-11
6
1,886 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:chrisfixit
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:xtravagan
ID: 24121969
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).
0
 
LVL 1

Author Comment

by:chrisfixit
ID: 24121989
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
0
 
LVL 15

Accepted Solution

by:
oobayly earned 300 total points
ID: 24122278
What you're looking to use is NtQuerySystemInformation (ntdll.dll) to get all the open handles on the system, then you can enumerate them until you find the one you want.

This code project article describes how to do it, unfortunately it's in C++
http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx?fid=422864&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=2277170
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Assisted Solution

by:xtravagan
xtravagan earned 200 total points
ID: 24125392
I think if you just do a Copy without removing, or open the file for writing and shove your stuff into it, it will work. Normally in windows you can rename locked files and copy into them (some can't)

Again it depends on how the application that owns the NewCopy file opened its file.

Shutting down the application isn't option? Or bootstrapping it to copy the file upon starting it?
0
 
LVL 1

Author Comment

by:chrisfixit
ID: 24154602
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.
0
 
LVL 5

Expert Comment

by:xtravagan
ID: 24154637
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/
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now