Path..::.GetTempFileName Method Home
This page is specific to:.NET Framework Version:1.12.03.03.5Silverlight 34.0
.NET Framework Class Library
Path..::.GetTempFileName Method

Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax

'Usage

Dim returnValue As String

returnValue = Path.GetTempFileName()

'Declaration

Public Shared Function GetTempFileName As String

Return Value

Type: System..::.String
A String containing the full path of the temporary file.
Exceptions

ExceptionCondition
IOException

An I/O error occurs, such as no unique temporary file name is available.

- or -

This method was unable to create a temporary file.

Remarks

This method creates a temporary file with a .TMP file extension.

The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files.

The GetTempFileName method will raise an IOException if no unique temporary file name is available. To resolve this error, delete all unneeded temporary files.

For a list of common I/O tasks, see Common I/O Tasks.

.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Other Resources

Community Content

GetTempFileName Method with Powershell
Added by:Thomas Lee
# get-GetTempFileName.ps1
# Creates a uniquely named, zero-byte file in the temp dir
# Thomas Lee - tfl@psp.co.uk

# Get file name (and create the file!):
$fn1 = [system.io.path]::GetTempFileName()

# Display Results
"File name returned: $fn1"
ls $fn1

# delete the file
Del $fn1

This script produces the following output:

PSH [C:\foo]: .\get-tempfilename.ps1
File name returned: C:\Documents and Settings\tfl\Local Settings\Temp\tmp6A.tmp


Directory: Microsoft.PowerShell.Core\FileSystem::C:\Documents and Settings\tfl\Local Settings\Temp


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/13/2008 7:41 PM 0 tmp6A.tmp


PSH [C:\foo]:
Also requires CAS EnvironmentPermission PermissionState.Unrestricted
Added by:Alan J. McFarlane

In low rights environments e.g. in some ASP.NET configurations, this function will fail with a security exception. That's because it needs to find the temp directory and that's reading the "Environment".

The exception stack shows that this method calls Path.GetTempPath which requires:

• EnvironmentPermission
for unrestricted access to environment variables. Associated enumeration: PermissionState.Unrestricted

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View