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
Solved

Which Process is holding the file

Posted on 2013-06-05
2
537 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

840 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