473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 7213
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

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

Similar topics

0
by: Markus Poehler | last post by:
Hi my program should run on terminal server. I open Acrobat process and I have to kill them at some points in my application. This fails cause of insufficient rights on terminal server. the...
2
by: Sri | last post by:
I am writing an asp.net applicaition using VB coding. In a function, I am opening an excel file with the following code, Dim objExcel As Object Dim objWorkBook As Object objExcel =...
4
by: Steve B. | last post by:
I think a user exited my local application incorrectly and now I can't compile a new version of the application to the public network without getting a "in use" error message. Also, can't rename...
8
by: shandra | last post by:
I have a file I need to delete or truncate. I tried using the KILL command in VB6. I tried using the file.delete command in VB.net. I tried manually deleting, renaming, and copying over the...
5
by: Dino Buljubasic | last post by:
My application can allow a user to open a file for viewing by fetching file data from database, creating the file in a temp directory and starting appropriate process (i.e. Adobe or any other...
0
by: Paul | last post by:
Hi, I'm trying to kick off the iiscnfg.vbs from a webservice to export a website's config to an xml file (And eventually populate other servers with the config). I initially tried this using the...
10
by: Susan | last post by:
I have a process that takes a while to run so I have it running asynchronously so that the user can continue working. My question is that I want to killl the process if the user changes the search...
3
by: eugenet | last post by:
Hello, This is probably a newbie question. In my C# program, when I try to delete a file, I get an exception that the file is locked by another process. I would like to terminate the process...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.