Solved

Which Process is holding the file

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

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Introduction to Processes
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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