By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,989 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,989 IT Pros & Developers. It's quick & easy.

How to determine which process to kill ???

P: n/a
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 depending of
file type) to open the file.

when I close my application, it is suppose to clean all files it
created in temp directory.

I do this by :
for each file in temp
File.Delete(file)
next

This works fine but if a file is open (say pdf file open by Adobe
process), I can not delete the file with this because the file is used
by Adobe.

How can I determine which process is using the file and kill it in
order to delete the file from temp directory.

I need something like this

for each file in temp
if file is open then
process = get process using this file
kill(process)
end if

delete file
next

Thank you
_dino_
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
1. When you create your temp files in your application, you should hold onto
the filenames that were used, like, in a string array or something:

' untested air code

Private _tmpFileList As New ArrayList()

Private Function createTempFile() As String

Dim filename As String = _
System.IO.Path.GetTempFileName()

_tmpFileList.Add(filename)

End Function

Private Sub cleanupTempFiles()

For Each filename As String In _tmpFileLilst
Try
File.Delete filename
' Catch -- do something here for FileNotFound or File is open
Finally
System.IO.Path.Delete(filename)
End Try
Next

End Sub

You could also accumulate your FileStream's or StreamReader or Writer in a
HashTable keyed off the filenames you get from createTempFile()
for each file in temp
if file is open then
process = get process using this file
kill(process)
end if

delete file
next


2. That right there sounds dangerous to me. What if you accidentally kill
some system related process or Windows Service or your AntiVirus agent(s).
You could destabilize the system. As a rule, your application should only
be deleting files that it created.

--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane
Nov 21 '05 #2

P: n/a
OK, the question is "What to do when file is open (i.e. held by
another process, for example by process used to open it)?"
What I need to do is to close it and then delete it. How can I do
that?
On Fri, 23 Sep 2005 15:09:55 -0400, "Mike Labosh"
<ml*****@hotmail.com> wrote:
1. When you create your temp files in your application, you should hold onto
the filenames that were used, like, in a string array or something:

' untested air code

Private _tmpFileList As New ArrayList()

Private Function createTempFile() As String

Dim filename As String = _
System.IO.Path.GetTempFileName()

_tmpFileList.Add(filename)

End Function

Private Sub cleanupTempFiles()

For Each filename As String In _tmpFileLilst
Try
File.Delete filename
' Catch -- do something here for FileNotFound or File is open
Finally
System.IO.Path.Delete(filename)
End Try
Next

End Sub

You could also accumulate your FileStream's or StreamReader or Writer in a
HashTable keyed off the filenames you get from createTempFile()
for each file in temp
if file is open then
process = get process using this file
kill(process)
end if

delete file
next


2. That right there sounds dangerous to me. What if you accidentally kill
some system related process or Windows Service or your AntiVirus agent(s).
You could destabilize the system. As a rule, your application should only
be deleting files that it created.


Nov 21 '05 #3

P: n/a
> OK, the question is "What to do when file is open (i.e. held by
another process, for example by process used to open it)?"
What I need to do is to close it and then delete it. How can I do
that?
I think you misunderstand. Your application should not go around killing
other processes just so you can delete their files. That should be the job
of the other process. If the other process that's not yours leaves files
cluttering up %TEMP% then you should manually delete them after politely
shutting down the other process. (Like File-> Exit or Restarting a Service
or Rebooting)

If the other process is one of your apps, change it's behavior and redeploy.

--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane
"Dino Buljubasic" <di**@noplacelikehome.com> wrote in message
news:34********************************@4ax.com...
On Fri, 23 Sep 2005 15:09:55 -0400, "Mike Labosh"
<ml*****@hotmail.com> wrote:
1. When you create your temp files in your application, you should hold
onto
the filenames that were used, like, in a string array or something:

' untested air code

Private _tmpFileList As New ArrayList()

Private Function createTempFile() As String

Dim filename As String = _
System.IO.Path.GetTempFileName()

_tmpFileList.Add(filename)

End Function

Private Sub cleanupTempFiles()

For Each filename As String In _tmpFileLilst
Try
File.Delete filename
' Catch -- do something here for FileNotFound or File is open
Finally
System.IO.Path.Delete(filename)
End Try
Next

End Sub

You could also accumulate your FileStream's or StreamReader or Writer in a
HashTable keyed off the filenames you get from createTempFile()
for each file in temp
if file is open then
process = get process using this file
kill(process)
end if

delete file
next


2. That right there sounds dangerous to me. What if you accidentally kill
some system related process or Windows Service or your AntiVirus agent(s).
You could destabilize the system. As a rule, your application should only
be deleting files that it created.

Nov 21 '05 #4

P: n/a
So would the right way be to simply close my application leaving say
pdf file open?

Because in that case, next time my application runs, if the user tries
to open the same pdf file and assuming that he/she has not closed it
after terminating my application I am going to run into the same
problem, this time trying to open a file that is already open.

Sorry but it is little bit confusing to me.

Thank you for your help

On Fri, 23 Sep 2005 15:28:56 -0400, "Mike Labosh"
<ml*****@hotmail.com> wrote:
OK, the question is "What to do when file is open (i.e. held by
another process, for example by process used to open it)?"
What I need to do is to close it and then delete it. How can I do
that?


I think you misunderstand. Your application should not go around killing
other processes just so you can delete their files. That should be the job
of the other process. If the other process that's not yours leaves files
cluttering up %TEMP% then you should manually delete them after politely
shutting down the other process. (Like File-> Exit or Restarting a Service
or Rebooting)

If the other process is one of your apps, change it's behavior and redeploy.


Nov 21 '05 #5

P: n/a
I guess i shoud simply then catch io exception when trying to open the
file and not process it or?

On Fri, 23 Sep 2005 15:28:56 -0400, "Mike Labosh"
<ml*****@hotmail.com> wrote:
OK, the question is "What to do when file is open (i.e. held by
another process, for example by process used to open it)?"
What I need to do is to close it and then delete it. How can I do
that?


I think you misunderstand. Your application should not go around killing
other processes just so you can delete their files. That should be the job
of the other process. If the other process that's not yours leaves files
cluttering up %TEMP% then you should manually delete them after politely
shutting down the other process. (Like File-> Exit or Restarting a Service
or Rebooting)

If the other process is one of your apps, change it's behavior and redeploy.


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.