?
Solved

Which Process is holding the file

Posted on 2013-06-05
2
Medium Priority
?
542 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
[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
2 Comments
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
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…

752 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