Solved

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

Posted on 2016-09-28
9
58 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

11 Experts available now in Live!

Get 1:1 Help Now