Solved

VB.NET - how to detect if a file is being used by another process

Posted on 2016-09-28
9
164 Views
Last Modified: 2016-10-02
Hi Experts,

How can I detect if a file is being used by another process and if so, delete/unlock it?

Regards,
Leigh
0
Comment
Question by:LeighWardle
  • 5
  • 2
  • 2
9 Comments
 
LVL 12

Assisted Solution

by:funwithdotnet
funwithdotnet earned 250 total points
ID: 41821021
Try to open the file. If it is locked/in use by another process, you will not be able to delete it while the other process has it.

Try
    File.Open( ...)
Catch ex as Exception
    ' Unable to open file. See ex for the exact issue.
End Try

Open in new window

0
 
LVL 1

Author Comment

by:LeighWardle
ID: 41821040
Thanks, funwithdotnet.

The user of my VB.NET app clicks a button which launches a batch file that creates the potentially troublesome file.
If the batch file crashes, the troublesome file is still locked by a process (the batch file?).

When the user of my VB.NET app clicks the button which launches a batch file again, he/she gets the error: locked/in use by another process.

So I cannot simply use the Try code with File.Open( ...) etc.
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 41821051
I will have the Process Id of the crashed batch file.
So if I Kill it, will it unlock the troublesome file?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41821053
You read my mind.

Not sure if that would work for your particular circumstance, but I'd definitely try it.

Good luck!
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 41821096
I did a little bit of debugging and my scenario is a bit more complicated.

As far as the VB.NET application is concerned the batch file does not crash, i.e. it terminates without error.

But logging of user error reports show that in some circumstances the batch file leaves behind a locked file.

So my problem is:  how can I use VB.NET to delete that file?
0
 
LVL 84
ID: 41821499
What does your Batch file do?
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 41822667
Hi Scott:

I used the term "batch file" loosely, my VB.NET app is launching a Command Shell CMD.EXE as follows:

excerpt of my VB.NET code:

       Dim objProcess As New Process
        Dim startInfo As New ProcessStartInfo("CMD.EXE")
        Dim boolUserHasInteruptedByESCKeyOnce As Boolean
        Dim objErrorMessage As clsErrorMessage


        startInfo.WindowStyle = ProcessWindowStyle.Minimized
        startInfo.WindowStyle = ProcessWindowStyle.Hidden
        startInfo.CreateNoWindow = True
        startInfo.UseShellExecute = False
        startInfo.Arguments = Arguments
        objProcess = Process.Start(startInfo)

Open in new window




Arguments will be something like:

/c ""D:\fortranpath\fortranapp.EXE" "jobname" "D:\fortranpath" "D:\datapath" 1>"D:\D:\datapath\maybelocked.sto" 2>&1"

maybelocked.sto is the file that can get locked.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 41823288
it terminates without error.

But logging of user error reports show that in some circumstances the batch file leaves behind a locked file.
It would seem, then, that the app that is fired by your Process (fortanapp.exe) would be the culprit. If your process terminates without error, then your process is not at fault. You'd have to determine why fortranapp.exe is keeping "maybelocked.sto" locked.
0
 
LVL 1

Author Closing Comment

by:LeighWardle
ID: 41825839
Hi Scott & funwithdotnet,

Thanks for your help.

I'm going to change my VB.NET code so that if the file is locked, I define a new one.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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