[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2016-09-28
9
Medium Priority
?
733 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 1000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 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