Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Using [IO.Path]::GetTempFileName()

Reply
 
Old 12-01-2006   #1 (permalink)
Marco Shaw


 
 

Using [IO.Path]::GetTempFileName()

..NET novice**

How does [IO.Path]::GetTempFileName() work? I'm trying to use it to avoid
some kind of race condition, but I need to call it, and get the resulting
temp file into a variable.

Then I'll come back later to actally use that temp file name by writing,
reading, and appending to it later. Eventually, I'll delete it.

I'm just wondering about the creation and deletion criteria, and how I make
sure I can't cause any conflicts by using multiple temp files.

Marco



My System SpecsSystem Spec
Old 12-01-2006   #2 (permalink)
Maximilian Hänel


 
 

Re: Using [IO.Path]::GetTempFileName()

Hi Marco,

> How does [IO.Path]::GetTempFileName() work? I'm trying to use it to avoid
> some kind of race condition, but I need to call it, and get the resulting
> temp file into a variable.
>
> Then I'll come back later to actally use that temp file name by writing,
> reading, and appending to it later. Eventually, I'll delete it.
>
> I'm just wondering about the creation and deletion criteria, and how I make
> sure I can't cause any conflicts by using multiple temp files.


[IO.Path]::GetTempFileName() internally calls the win32 API function
GetTempFileName. To avoid race conditions GetTempFileName actually
creates an empty file with the "temp file name". So if a file with that
name already exists (that is creating a _new_ file fails) an other name
is choosen until creating a new file with that name succeeds.
Well, at least that's how I would say GetTempFileName works...

btw By using the following expression you can clearly see that each call
to GetTempFileName actually creates an empty file:

ls ([IO.Path]::GetTempFileName())

hth

Max
My System SpecsSystem Spec
Old 12-01-2006   #3 (permalink)
Keith Hill [MVP]


 
 

Re: Using [IO.Path]::GetTempFileName()

"Marco Shaw" <marco@Znbnet.nb.ca> wrote in message news:eLNV4KbFHHA.3540@TK2MSFTNGP02.phx.gbl...
> .NET novice**
>
> How does [IO.Path]::GetTempFileName() work?


It creates the file as a zero length file with a guaranteed unique name.

"Hello" > $tempFile
"World" >> $tempFile

remove-item $tempFile

> I'm just wondering about the creation and deletion criteria, and how I make
> sure I can't cause any conflicts by using multiple temp files.


I don't think you will run into problems since GetTempFilename assures that you get a unique name. You could even use a trap to make sure the file get's cleaned up e.g.:

trap { if ($tempFile -ne $null) { remove-item $tempFile }; break}

$tempFile = [IO.Path]::GetTempFilename()
gci $tempFile
"Hello" > $tempFile
"World" >> $tempFile
gc $tempFile
gci $tempFile
throw "foo"
ri $tempFile
gci $tempFile

--
Keith
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to know DLL path from itself .NET General
split-path -> unc-path PowerShell
BUG? (Test-Path $path -IsValid) and empty $path PowerShell
BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal path PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46