• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 917
  • Last Modified:

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

Hi Experts,

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

Regards,
Leigh
0
LeighWardle
Asked:
LeighWardle
  • 5
  • 2
  • 2
2 Solutions
 
funwithdotnetCommented:
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
 
LeighWardleAuthor Commented:
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
 
LeighWardleAuthor Commented:
I will have the Process Id of the crashed batch file.
So if I Kill it, will it unlock the troublesome file?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
funwithdotnetCommented:
You read my mind.

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

Good luck!
0
 
LeighWardleAuthor Commented:
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
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
What does your Batch file do?
0
 
LeighWardleAuthor Commented:
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
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
 
LeighWardleAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now