Link to home
Start Free TrialLog in
Avatar of purejamie
purejamieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

system.io.path.GetTempFileName - Can't delete it!

Hello Experts,

Hopefully this one will be an easy one for someone!

I am creating a temp file using  'system.io.path.gettempfilename' to use within a sub, however, when I try to delete it at the end of the sub it returns the message - "The process can not acces the file <filename I  created earlier> because it is being used by another process" . In a nutshell, the code would look like this:

dim strFilename as string = system.io.path.gettempfilename

<Do stuff with the file, in this case using it to generate new transforms for MSI's>

system.io.file.delete(strfilename)

I have tried using marshal and garbage collection to release the file for deletion with no luck. Any Comcontrol's etc. that could possibly be holding the file open are also closed. Any help would be greatly appreciated.

Cheers,
Jamie.
Avatar of DjDezmond
DjDezmond
Flag of United Kingdom of Great Britain and Northern Ireland image

Have you tried running the code without the delete command, waiting a few seconds and then running only the delete command? The code might just be moving to fast to wait for the file to be released. A sleep command would be useful in this case, but obviously a Timer control would have to be used as Vb.Net doesn't have a sleep function.

Dez
Let's start with something simple...

Did you perform a "close" operation on the FileStream/TextWriter/etc that you used to write to the file?
Avatar of purejamie

ASKER

Hi there,

The gettempfilename command creates a 0 byte file on the disk and passes back the path as the string. This is then used as a path for some Msi Transform stuff. The file is not created as an object in the traditional sense  - therefore, doing a strFileName.close will not work (as it is a string). I'm afriad waiting doesn't make a difference either - any operations are definately complete on the file by this time. I think it is something to do with how the tempfile is created?

Cheers,
Jamie.
Hello again,

Thanks again for the replies. I am using the file in the following way - (strTempMsiFilename is the temp file created as above)

Dim objDatabase As Object = objInstaller.opendatabase(strTempMsiFileName, 1)

At the end of the sub I have cleared objDatabase.

Thanks for any help.
Cheers,
Jamie.
Sorry, one more thing - DjDez - you can sleep threads using 'system.threading.thread.sleep' if that is any use to you!?

Cheers,
Jamie.
yea, i forgot all about threading!

Cheers

Dez
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi guys,

It is now sorted! I had fully commited the database changes but it got me looking at the comObjects again, I then realised my mistake (hadn't released *all* the comobjects used).. So thanks for getting me to look in the right direction ;) !!!

Cheers,
Jamie.