3 out of 11 rated this helpful - Rate this topic

GetTempFileName function

Applies to: desktop apps only

Creates a name for a temporary file. If a unique file name is generated, an empty file is created and the handle to it is released; otherwise, only a file name is generated.

Syntax

UINT WINAPI GetTempFileName(
  __in   LPCTSTR lpPathName,
  __in   LPCTSTR lpPrefixString,
  __in   UINT uUnique,
  __out  LPTSTR lpTempFileName
);

Parameters

lpPathName [in]

The directory path for the file name. Applications typically specify a period (.) for the current directory or the result of the GetTempPath function. The string cannot be longer than MAX_PATH–14 characters or GetTempFileName will fail. If this parameter is NULL, the function fails.

lpPrefixString [in]

The null-terminated prefix string. The function uses up to the first three characters of this string as the prefix of the file name. This string must consist of characters in the OEM-defined character set.

uUnique [in]

An unsigned integer to be used in creating the temporary file name. For more information, see Remarks.

If uUnique is zero, the function attempts to form a unique file name using the current system time. If the file already exists, the number is increased by one and the functions tests if this file already exists. This continues until a unique filename is found; the function creates a file by that name and closes it. Note that the function does not attempt to verify the uniqueness of the file name when uUnique is nonzero.

lpTempFileName [out]

A pointer to the buffer that receives the temporary file name. This buffer should be MAX_PATH characters to accommodate the path plus the terminating null character.

Return value

If the function succeeds, the return value specifies the unique numeric value used in the temporary file name. If the uUnique parameter is nonzero, the return value specifies that same number.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

The following is a possible return value.

Return valueDescription
ERROR_BUFFER_OVERFLOW

The length of the string pointed to by the lpPathName parameter is more than MAX_PATH–14 characters.

 

Remarks

The GetTempFileName function creates a temporary file name of the following form:

<path>\<pre><uuuu>.TMP

The following table describes the file name syntax.

ComponentMeaning
<path>Path specified by the lpPathName parameter
<pre>First three letters of the lpPrefixString string
<uuuu>Hexadecimal value of uUnique

 

If uUnique is zero, GetTempFileName creates an empty file and closes it. If uUnique is not zero, you must create the file yourself. Only a file name is created, because GetTempFileName is not able to guarantee that the file name is unique.

Only the lower 16 bits of the uUnique parameter are used. This limits GetTempFileName to a maximum of 65,535 unique file names if the lpPathName and lpPrefixString parameters remain the same.

Due to the algorithm used to generate file names, GetTempFileName can perform poorly when creating a large number of files with the same prefix. In such cases, it is recommended that you construct unique file names based on GUIDs.

Temporary files whose names have been created by this function are not automatically deleted. To delete these files call DeleteFile.

To avoid problems resulting when converting an ANSI string, an application should call the CreateFile function to create a temporary file.

Examples

For an example, see Creating and Using a Temporary File.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

GetTempFileNameW (Unicode) and GetTempFileNameA (ANSI)

See also

File Management Functions
Naming Files, Paths, and Namespaces
CreateFile
DeleteFile
GetTempPath

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Not suitable for robust apps
I recommend this API only for useful for casual use. Because of the 65535 limit on files, if a poorly behaved app leaks temp files, you will get to a point where your call will fail. It also sometimes is not reliable under stress.
A robust app should do something like this instead (here in C#)
string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))
This is fast, reliable, and also avoids creating a zero byte file first, then doing another IO to put content into it.
For .NET, see Path.GetTempFileName
System.IO.Path exposes this in managed code, complete with a demand for permission, etc:

[System.Security.SecuritySafeCritical]
[ResourceExposure(ResourceScope.AppDomain)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
public static String GetTempFileName()


For completeness, here's the P/Invoke definition I wrote in mscorlib. Compared with the other P/Invoke wrappers posted here, note the other important DllImport flags, as well as the return value which you must check to see if an error occurred.

[DllImport(KERNEL32, CharSet=CharSet.Auto, SetLastError=true, BestFitMapping=false)]
[ResourceExposure(ResourceScope.None)]
internal static extern uint GetTempFileName(String tmpPath, String prefix, uint uniqueIdOrZero, StringBuilder tmpFileName);
vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Public Shared Sub GetTempFileName(ByVal tempDirName As String, ByVal prefixName As String, ByVal unique As Integer, ByVal sb As StringBuilder)
End Sub
C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern void GetTempFileName(string tempDirName, string prefixName, int unique, StringBuilder sb);
Maximum of 65535 temporary files
You can only have 65535 temporary files with a specific prefix in one folder. This is mentioned in the documentation for the managed version of GetTempFileName - http://msdn2.microsoft.com/en-au/library/system.io.path.gettempfilename.aspx