This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 1.1+

Generating Temporary Files

When you need to store information for a short period of time, you may decide to hold the data in a temporary file located on the user's hard disk drive. The .NET framework provides two standard methods that assist in selecting a temporary file name.

Path Class

The Path class is a standard utility class within the .NET framework's System.IO namespace. It provides a set of static methods and fields that perform functions related to the processing of file names and paths. Included in the class are two methods that generate randomised temporary file names.

The Path class is found in the System.IO namespace, so the code can be made simpler by adding the following using directive:

using System.IO;

Generating a Temporary File

The first Path member that we will consider is GetTempFileName. Despite the name, this method not only generates a new randomised file name, but also creates an empty file of that name in the user's temp folder. The name of the file is returned as a string that also contains the full path to the folder in which it is stored. This information can then be used to open the file and write the desired information into it.

The file name selected for the new file has an extension of "tmp". The name includes four hexadecimal characters that distinguish it from any other temporary files. This creates a limit on the number of files that may be created before existing files must be removed from the user's temp folder. The limit is 65,535 files. If this number is exceeded, the method throws an IOException.

The following code generates a new file and outputs its name. The name will vary from that shown. After executing the code, examine the temporary folder to find the new zero-length file, which can be deleted.





Generating a Temporary File Name

If you are using .NET framework version 2.0, a second method exists for obtaining a randomised temporary file name. This method is named GetRandomFileName. Unlike GetTempFileName, this second method simply generates a new file name and returns it as a string. No file is created and the name returned does not include a path.

GetRandomFileName creates a name containing eleven alphanumeric characters in the DOS 8.3 format. This provides a much larger range of possible names than the GetTempFileName method. The name is cryptographically secure and should be used when the security of the user's file system is important.

The name returned by GetRandomFileName can be used for both files and folders.

Console.WriteLine(Path.GetRandomFileName());    // Outputs "2wq1g41s.enm"

If you wish to use this method to generate a file name that includes the path to the user's temp folder, you should combine it with a call to the GetTempPath method, which returns the required path. This is demonstrated in the following example. In this case, the two elements are joined together using Path.Combine.

Console.WriteLine(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));



25 November 2008