![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||