Solved

Which Process is holding the file

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 56
strCopies  challenge 17 73
Can not delete, remove this shortcut, file 4 25
MSP multi use software 4 79
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
The viewer will learn common shortcuts with easy ways to remember them. The viewer will then learn where to find all of the keyboard shortcuts, how to create/change them, and how to speed up their workflow.
Viewers will learn how to use the Hootsuite Dashboard.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now