Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Nov 2008
Posts: 14
#1: Nov 19 '08
How do i find a process that has a file locked and kill it?

I am using VB.net

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Nov 19 '08

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


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
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,392
#3: Nov 19 '08

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


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

http://technet.microsoft.com/en-us/s.../bb896653.aspx
Newbie
 
Join Date: Nov 2008
Posts: 14
#4: Nov 19 '08

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


i'd like to do it programmatically through the code
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#5: Nov 19 '08

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


As far as I know, the only way to get to this information is through P/Invoke calls to NtQuerySystemInformation. 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.
Newbie
 
Join Date: Nov 2008
Posts: 14
#6: Nov 20 '08

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


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,application).exit() and it didn't work.


private WordProcessID as int32
private oWord As object
....
private sub CreateWordProcess()
Dim preExistingProcess As Boolean
If Me.WordProcessID > 0 Then
Try
Dim pr As Process = Process.GetProcessById(WordProcessID)
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.GetProcessesByName("WINWORD")

oWord = CreateObject("Word.Application")


Dim plist2 As Process() = Process.GetProcessesByName("WINWORD")






For Each p2 As Process In plist2
preExistingProcess = False
For Each p1 As Process In plist1
If p1.Id = p2.Id Then
preExistingProcess = True
Exit For
End If
Next
If preExistingProcess = False Then
Me.WordProcessID = p2.Id
Exit For
End If
Next p2
End
end sub

Private Sub KillWordProcess()
If WordProcessID > 0 Then
oWord = Nothing
Process.GetProcessById(WordProcessID).Kill()
End If
End Sub
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#7: Nov 20 '08

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


If you created the word process...shouldn't you then have the correct objects/methods needed to close it?
Newbie
 
Join Date: Nov 2008
Posts: 14
#8: Nov 20 '08

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


when i inherited this code it used this

Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. 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.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#9: Nov 20 '08

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


I would break it up then, not sure on vb syntax
Expand|Select|Wrap|Line Numbers
  1. declare myword as Word._Application
  2. myword= CType(oWord, Word._Application)
  3.  
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?
Reply


Similar .NET Framework bytes