.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

Visual Basic (Declaration)
Public Shared Function GetTempFileName As String
Visual Basic (Usage)
Dim returnValue As String

returnValue = Path.GetTempFileName()
C#
public static string GetTempFileName()
Visual C++
public:
static String^ GetTempFileName()
JScript
public static function GetTempFileName() : 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

Tags :


Community Content

Thomas Lee
GetTempFileName Method with Powershell
# 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]:

Alan J. McFarlane
Also requires CAS EnvironmentPermission PermissionState.Unrestricted

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

Tags : contentbug

Page view tracker