Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-11
6
Medium Priority
?
2,102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Suggested Courses

670 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