Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Creating Temporary Files Using Win32 System Services

Author: DevASP
Download Source Code : 626_TemporaryFiles

In every serious software development we often need to create temporary files. These files are normally created in this directory “\\Documents and Settings\User Name\Local Settings\Temp” These files

These files have randomly generated names. In this Article I will discuss two Win32 system services API’s GetTempPath and GetTempFileName.The APIs contains two functions that make it easy to create temporary files. The first function retrieves the path to the Windows temporary directory, while the second creates a unique temporary file in that directory.



DWORD GetTempPath(DWORD bufferSize,LPTSTR buffer);


bufferSize                      Maximum size of the buffer

Buffer                            The buffer that holds the path

Returns the number of characters placed in the buffer or, if greater than bufferSize, the required size of the buffer.



UINT GetTempFileName(LPCTSTR path,LPCTSTR prefix,UINT unique,

LPTSTR tempFile);


Path                 The path for the new temporary file

Prefix                The prefix used on the new file name

Unique              A number, either zero or unique

tempFile            Buffer to hold the new name, at least MAX_PATH long 


Returns the unique number used in the name, or 0 on error

GetTempFileName returns a valid and unique name for creating a temp file. Infect a lot of applications can create temp files in the same temp directory so we must have to distinguish our files from other file so that after the use of their we could delete them. So we could specify a prefix up to three characters that our GetTempFileName will placed at the beginning of our temporary created file name. An application should use the consistent temp prefix so that it can easily clean up them later.A unique number can be either zero or any other you choose, if you chose it zero system will pich a number that will generate a unique name for the file. When unique is zero, the system automatically creates the new file in the temporary directory and closes it. When you supply a non-zero value for unique, you should create the new temporary file with CreateFile.The code sample I have written for this is looks like this



const int BUFFSIZE = 1000;

      DWORD pathSize;

      char pathBuffer[BUFFSIZE];

      char tempFilename[MAX_PATH];

      UINT uniqueNum;

      pathSize = GetTempPath( BUFFSIZE, pathBuffer);

      if (pathSize < BUFFSIZE)

            pathBuffer[pathSize] = 0;


            pathBuffer[0] = 0;



      uniqueNum = GetTempFileName(pathBuffer, "smp",FILE_FLAG_DELETE_ON_CLOSE , tempFilename);


      char buffer[100];




This code will show you the path of temp directory that the system has get by GetTempPath() API. And also show you the name of the file the system has generated for you and the number which system has pick to generate the unique file name. Since unique is zero in this code, you can also look in the temp directory and see that the new file exists.

Add Article Comment:
Name :
Email Address :
Comments :
<< How to Copy, Move, and delete a file using win32 system services

Disclaimer - Privacy
© 2002-2018