O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Create a Temporary File

Problem

You want to get a file name that you can use for a temporary file.

Solution

Use the shared Path.GetTempFileName method.

Discussion

There are a number of approaches to generating temporary files. In simple cases, you might just create a file in the application directory, possibly using a GUID filename or a timestamp in conjunction with a random value. However, the System.IO.Path class provides a helper method that can save you some work. It returns a unique filename (in the current user’s temporary directory) that you can use to create a file for storing temporary information. This might be a path like c:\documents and settings\username\local settings\temp\tmpac9.tmp.

Dim TempFile As String = Path.GetTempFileName() Console.WriteLine("Using ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required