Solved

Which Process is holding the file

Posted on 2013-06-05
2
533 Views
Last Modified: 2013-06-20
I have an application that moves a file then deletes the original and occasionally it will error on the delete, I am able to detect that the file is still in use by a process but I need to know which one it is with the following code so that I can 'kill' it. The following code errors when it gets to the *** line of code with "Access is denied" and the return array is always blank.

VB.Net 2008

Private Function getFileProcesses(ByVal strFile As String) As ArrayList
        Try
            myProcessArray.Clear()
            Dim processes As Process() = Process.GetProcesses
            Dim i As Integer
            For i = 0 To processes.GetUpperBound(0) - 1
                myProcess = processes(i)
                Try
                    If Not myProcess.HasExited Then *** Errors here
                        Try
                            Dim modules As ProcessModuleCollection = myProcess.Modules
                            Dim j As Integer
                            For j = 0 To modules.Count - 1
                                If (modules.Item(j).FileName.ToLower.CompareTo(strFile.ToLower) = 0) Then
                                    myProcessArray.Add(myProcess)
                                    Exit For
                                End If
                            Next j
                        Catch exception As Exception
                            'Do Nothing
                        End Try
                    End If
                Catch ex As Exception
                    'Do Nothing
                End Try
            Next i
        Catch ex As Exception
            'Do Nothing
        End Try
        Return myProcessArray
    End Function


Any help would be appreciated.
0
Comment
Question by:scprogs
2 Comments
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39225017
there is no api for this. you would need to evaluate all handles of all processes what is not possible because your program doesn't have the rights to do that.

often it is the Windows Explorer which blocks a file or folder. also 'killing' a process not necessarily would help, cause killing may prevent from properly closing the file handles and is therefore counterproductive to your goal.

what you can do to keep the problem small is to avoid that it is your own process, ide, debugger, explorer window, programs that were responsible for the lock. so if you have multi-threading you always should terminate the threads softly (and not killing them). you alway should close files after use. you also could try to 'move' the original file before operation. that way, you could find out whether the file was locked before your own task and give an appropriate warning for the user. another option you have, is to add the file to the SessionManager\PendingFileRenameOperations in the registry. with that the file could be deleted at restart of the system (see http://technet.microsoft.com/en-us/library/cc960241.aspx).

Sara
0
 
LVL 7

Expert Comment

by:scraby
ID: 39245904
there is a program that i've used often and works well that finds what process has a file tied up and gives you some options such as unlock the file from the process or kill the process and so on.  you can find it here:

http://www.emptyloop.com/unlocker/

that's about all i can tell you.  maybe you could get in touch with the guy that wrote it (cedrick 'nitch' collomb is his name) google him and maybe you'll get somewhere that way
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

777 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