Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-11
6
Medium Priority
?
2,137 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 900 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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 5

Assisted Solution

by:xtravagan
xtravagan earned 600 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

824 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