Avatar of purejamie
purejamie
Flag 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.
Visual Basic.NET

Avatar of undefined
Last Comment
purejamie

8/22/2022 - Mon
DjDezmond

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
graye

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?
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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
purejamie

ASKER
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.
purejamie

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

Cheers,
Jamie.
DjDezmond

yea, i forgot all about threading!

Cheers

Dez
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
graye

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
purejamie

ASKER
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.