5.22. Get a Random File Name

Problem

You need to get a random name for creating a folder or a file.

Solution

Use the Path.GetRandomFileName method, which returns a random name.

How It Works

The System.IO.Path class includes a GetRandomFileName method that generates a random string that can be used for creating a new file or folder. The difference between GetRandomFileName and GetTempFileName (discussed in recipe 5-16) of the Path class is that GetRandomFileName just returns a random string and does not create a file, whereas GetTempFileName creates a new 0-byte temporary file and returns the path to the file.

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.