How do i find a process that has a file locked and kill it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hd95
    New Member
    • Nov 2008
    • 24

    How do i find a process that has a file locked and kill it?

    How do i find a process that has a file locked and kill it?

    I am using VB.net
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well I am sure you could dig through win32_API to find this out, but I would download and use ProcessExplorer and do it from there

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      you can try using a tool like process explorer...

      Find out what files, registry keys and other objects processes have open, which DLLs they have loaded, and more.

      Comment

      • hd95
        New Member
        • Nov 2008
        • 24

        #4
        i'd like to do it programmaticall y through the code

        Comment

        • nukefusion
          Recognized Expert New Member
          • Mar 2008
          • 221

          #5
          As far as I know, the only way to get to this information is through P/Invoke calls to NtQuerySystemIn formation. Most of the API required to perform this task is only loosely documented, so good luck!

          There is an excellent discussion here that may help you achieve what you want.

          However, killing off a process that has an open file handle is obviously not a recommended practice. Depending on what is being done with the file at the time your possibly risking corruption. If there is any other way to achieve what you want, I'd probably do that instead.

          Comment

          • hd95
            New Member
            • Nov 2008
            • 24

            #6
            here's the method i came up with...

            I have a word to text file conversion program that uses an instance of word and there might be other instances running as well. My app needs to recreate the process if someone cancels it or cancel the one its using (and none others) on exit. My solution was to get 2 lists of the word instances that are running, 1 before I create the new instance and 1 after. I then compare the 2 lists to get the new process id and store it in a global so i know which one to kill it later. If someone knows a cleaner way please tell.
            I was trying ctype(oword,app lication).exit( ) and it didn't work.


            private WordProcessID as int32
            private oWord As object
            ....
            private sub CreateWordProce ss()
            Dim preExistingProc ess As Boolean
            If Me.WordProcessI D > 0 Then
            Try
            Dim pr As Process = Process.GetProc essById(WordPro cessID)
            If pr.Responding = False Then
            pr.Kill()
            oWord = Nothing
            End If
            Catch ex As Exception
            oWord = Nothing
            End Try


            End If

            If oWord Is Nothing Then

            Dim plist1 As Process() = Process.GetProc essesByName("WI NWORD")

            oWord = CreateObject("W ord.Application ")


            Dim plist2 As Process() = Process.GetProc essesByName("WI NWORD")






            For Each p2 As Process In plist2
            preExistingProc ess = False
            For Each p1 As Process In plist1
            If p1.Id = p2.Id Then
            preExistingProc ess = True
            Exit For
            End If
            Next
            If preExistingProc ess = False Then
            Me.WordProcessI D = p2.Id
            Exit For
            End If
            Next p2
            End
            end sub

            Private Sub KillWordProcess ()
            If WordProcessID > 0 Then
            oWord = Nothing
            Process.GetProc essById(WordPro cessID).Kill()
            End If
            End Sub

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              If you created the word process...shoul dn't you then have the correct objects/methods needed to close it?

              Comment

              • hd95
                New Member
                • Nov 2008
                • 24

                #8
                when i inherited this code it used this

                Code:
                CType(oWord, Word._Application).Quit()
                For whatever reason that compiled originally and was put into production and continues to work. At this point the same code refuses to compile.

                I tried this code which does compile but doesn't work
                Code:
                CType(oWord, Application).Exit()
                I scoured the net looking for a solution till I came up with with the work around above. I'd love to have a cleaner method.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  I would break it up then, not sure on vb syntax
                  [code=vb.net]
                  declare myword as Word._Applicati on
                  myword= CType(oWord, Word._Applicati on)
                  [/code]
                  Actually I guess you could set a breakpoint and see what the object type of oWord is.
                  Then cast it to its correct type, then perform some actions on that type like Quit() or something else?

                  Comment

                  Working...