Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Using [IO.Path]::GetTempFileName()

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-01-2006   #1 (permalink)
Marco Shaw
Guest


 

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
Guest


 

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]
Guest


 

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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
split-path -> unc-path lerni PowerShell 4 07-31-2008 04:27 AM
UNC Path microsvc Vista General 0 04-01-2008 12:39 PM
BUG? (Test-Path $path -IsValid) and empty $path =?Utf-8?B?Um9tYW4gS3V6bWlu?= PowerShell 1 08-28-2006 12:10 PM
BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal path Adam Milazzo [MSFT] PowerShell 2 08-12-2006 03:14 AM
Binding(string path) - what is the syntax for the path? Jason Dolinger Avalon 2 01-10-2006 03:52 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008