Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
Path Class
Path Methods
 GetTempFileName Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.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)
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.
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.

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.

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.

.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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
GetTempFileName Method with Powershell      Thomas Lee   |   Edit   |   Show History
# 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      Alan J. McFarlane   |   Edit   |   Show History

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

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker