C# Path.GetRandomFileName Method

You want to save data to the disk so it can be read in later, but the filename is not important. The filename should be random so it does not conflict with other files and will be ignored and not corrupted by other programs. The Path.GetRandomFileName method offers this functionality in the C# language. Here we look at an example of using Path.GetRandomFileName to generate three files with random file names, and also look more in-depth at the System.IO namespace method.

Random file names seen in Windows Explorer

Generating random file names

First, there are many different ways of generating random strings and file names in the C# language. You could develop a method that populates a char array using the Random class, and then returns a string based on those numbers. However, the .NET Framework and base class library offers the excellent Path.GetRandomFileName static parameterless method, which returns appropriate strings and is very reliable.

=== Program that writes random file names (C#) ===

using System;
using System.IO;

class Program
{
    static void Main()
    {
        //
        // Write three random files.
        //
        WriteRandomFile();
        WriteRandomFile();
        WriteRandomFile();
    }

    static void WriteRandomFile()
    {
        //
        // Get random file name.
        //
        string fileName = Path.GetRandomFileName();
        //
        // Construct full path.
        //
        string outputPath = Path.Combine("C:\\", fileName);
        //
        // Write to screen and disk.
        //
        Console.WriteLine(fileName);
        File.WriteAllText(outputPath, "Random");
    }
}

=== Output of the program ===

m4adstid.dw2
mbhbtcen.5m1
1qtk3r5u.bka

Overview of the example. The Program class defines two methods, the Main entry point and the WriteRandomFile method. Control flow begins in Main and the WriteRandomFile is called three times sequentially. The WriteRandomFile method internally gets a random file name, gets the correct output path on the C:\ volume, and then writes some text to it.

Path.GetRandomFileName method. The Path.GetRandomFileName method in the above program returns a random file name with an extension. This includes the "." part before the extension. The extension will be 1 dot and 3 letters, while the name will be 8 characters. All the characters are valid file name characters.

Internal implementation [.NET 3.5 SP1]

Internally, the Path.GetRandomFileName method gets random bytes from the cyptographic-quality random number generator RNGCryptoServiceProvider. Next, it uses bitwise ANDs to fill a StringBuilder with characters based on those bytes. These methods will require significant CPU time to execute.

The author's notes. The ToBase32String method uses a StringBuilder to generate the file name. When that method returns, the StringBuilder is converted to a string. In the calling method, the string is converted to a char array. Finally, that char array is converted to a string. This method could probably be optimized significantly.

Summary

Here we looked at how you can get random file names to store certain kinds of data. By using Path.GetRandomFileName, you can write to files that have cryptographically-secure random names. This can enhance security or simplicity in your programs. We saw that the method uses the RNGCryptoServiceProvider class. Finally, it is often easier to base file names on the date and time.

See DateTime for Filenames.

See Path Tutorials.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen