469,360 Members | 1,577 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

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

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

I am using VB.net
Nov 19 '08 #1
8 6892
Plater
7,872 Expert 4TB
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
Nov 19 '08 #2
RedSon
5,000 Expert 4TB
you can try using a tool like process explorer...

http://technet.microsoft.com/en-us/s.../bb896653.aspx
Nov 19 '08 #3
hd95
24
i'd like to do it programmatically through the code
Nov 19 '08 #4
nukefusion
221 Expert 100+
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.
Nov 19 '08 #5
hd95
24
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
Nov 20 '08 #6
Plater
7,872 Expert 4TB
If you created the word process...shouldn't you then have the correct objects/methods needed to close it?
Nov 20 '08 #7
hd95
24
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.
Nov 20 '08 #8
Plater
7,872 Expert 4TB
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?
Nov 20 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Markus Poehler | last post: by
4 posts views Thread by Steve B. | last post: by
5 posts views Thread by Dino Buljubasic | last post: by
10 posts views Thread by Susan | last post: by
3 posts views Thread by eugenet | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.