Solved

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

Posted on 2009-04-11
6
1,864 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

758 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

20 Experts available now in Live!

Get 1:1 Help Now