Solved

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

Posted on 2016-09-28
9
347 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
[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
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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 85
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 85

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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