How to determine which process to kill ???

  • Thread starter Thread starter Dino Buljubasic
  • Start date Start date
D

Dino Buljubasic

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_
 
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
 
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?
 
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
 
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
 
I guess i shoud simply then catch io exception when trying to open the
file and not process it or?
 
Back
Top