C# Path GetRandomFileName() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Path.GetRandomFileName() returns a random name that you can use for a file or a folder. The name has the form abcdefgh.xyz: eight characters, a dot, and three more, so it is always 12 characters long. It uses lowercase letters and digits.

The random part comes from a cryptographically strong source, so guessing the next name is not practical. The method makes nothing on disk and checks nothing: it only returns text. Unlike GetTempFileName, it does not choose a folder for you.

This is a static method of System.IO.Path. Call it as Path.GetRandomFileName(...); there is no new Path(). It only returns text. It does not create a file or folder, and it does not check whether the name is already used.

Syntax

The method takes no arguments. Add a folder yourself when you need a path.

C#
string name = Path.GetRandomFileName();                       // e.g. ojmeoe5m.q4q
string path = Path.Combine(Path.GetTempPath(), name);

Console.WriteLine(name.Length);   // 12
CallWhat it does
Path.GetRandomFileName()A new random 8.3 name every time, such as k3x9a1bq.m7t.
Path.Combine(Path.GetTempPath(), name)Turns the name into a path in the temp folder.
Path.GetTempFileName()Also random, but it creates the file and returns a full path.
Guid.NewGuid().ToString("N")Another way to get a unique name, 32 characters long and without a dot.

The name is random, so the output differs on each call: tutorials print its shape (length, dot, characters), never the name. Two calls can be equal only by an extremely small chance, but for a file in a shared folder you should still create with FileMode.CreateNew so a clash fails loudly.

Library declarations
C#
public static string GetRandomFileName();

Parameters

ParameterDescription
NoneGetRandomFileName() takes no parameters.

Return Value

Return ValueDescription
stringA 12-character random name in the form name.ext (eight characters, a dot, three characters) made of lowercase letters and digits.

C# Examples

Example 1 — Check the Shape of a Random Name

The name changes on every run, so these checks print what is always true about it.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string name = Path.GetRandomFileName();

        Console.WriteLine("Length 12: " + (name.Length == 12));
        Console.WriteLine("Dot at index 8: " + (name[8] == '.'));
        Console.WriteLine("Stem length: " + Path.GetFileNameWithoutExtension(name).Length);
        Console.WriteLine("Extension length: " + Path.GetExtension(name).Length);
        Console.WriteLine("Lower case: " + (name == name.ToLowerInvariant()));
    }
}

How It Works

  1. Always 12 characters. The format is fixed at eight characters, a dot, and three characters, so the length check is True every time.
  2. The dot sits at index 8. Positions start at zero, so the eight random characters take positions 0 to 7, and the dot is next.
  3. Reuse the Path methods. GetFileNameWithoutExtension returns the eight characters before the dot. GetExtension returns four, because the dot is included.
  4. No capitals. The name uses lowercase letters and digits, so changing it to lower case gives the same text. The name itself is never printed, because it is different each time.

Example 2 — Valid Characters, Different Every Time

Check that the name is safe to use as a file name and that two calls give two different names.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string a = Path.GetRandomFileName();
        string b = Path.GetRandomFileName();

        Console.WriteLine("Different: " + (a != b));
        Console.WriteLine("Valid as file name: " + (a.IndexOfAny(Path.GetInvalidFileNameChars()) < 0));
        Console.WriteLine("Same file name after GetFileName: " + (Path.GetFileName(a) == a));
        Console.WriteLine("Has extension: " + Path.HasExtension(a));
    }
}

How It Works

  1. Two calls, two names. Eleven random characters, each a letter or a digit, make a match extremely unlikely, so the first line is True in practice.
  2. Safe characters only. IndexOfAny finds none of the invalid file name characters in the name, so the result is below zero and the line is True.
  3. No separator inside. GetFileName returns the same text, so the name has no folder part and cannot move a file into another folder.
  4. It looks like a file name. The three characters after the dot are a real extension, so HasExtension is True. If you do not want an extension, use GetFileNameWithoutExtension on it.

Example 3 — Create a Unique Folder From a Random Name

The name alone does not create anything. Show that, then make the folder and remove it.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string name = Path.GetRandomFileName();
        string folder = Path.Combine(Path.GetTempPath(), "codetofun-rand-" + name);

        Console.WriteLine("Before: " + Directory.Exists(folder));

        Directory.CreateDirectory(folder);
        Console.WriteLine("After CreateDirectory: " + Directory.Exists(folder));
        Console.WriteLine("Name part length: " + Path.GetFileName(folder).Length);

        Directory.Delete(folder);
        Console.WriteLine("After Delete: " + Directory.Exists(folder));
    }
}

How It Works

  1. A path from a random name. The prefix codetofun-rand- plus the random name goes into the temp folder. Nothing exists yet, so the first check is False.
  2. CreateDirectory does the work. The random name only made a path. After CreateDirectory, the folder is real, so the check is True.
  3. Count the last part. The prefix has 15 characters and the random name has 12, so GetFileName(folder) is 27 characters long. The text itself is not printed.
  4. Remove it. The folder is empty, so a plain Directory.Delete is enough. The last check confirms that it is gone.

Frequently Asked Questions

A random name in 8.3 format, such as abcdefgh.xyz: eight characters, a dot, and three characters, 12 in all. It does not create a file.
No. It only returns a string. Use File.WriteAllText or Directory.CreateDirectory to create something with that name, or use GetTempFileName if you want the file reserved for you.
The name comes from a cryptographically strong random source, so it is hard to predict. That helps against guessing. It is still not a secret token: do not use it as a password.
GetRandomFileName returns only a name, in no folder, and creates nothing. GetTempFileName creates an empty .tmp file in the temp folder and returns its full path.
Yes. The name is valid for a folder as well as for a file, and the dot is allowed. Combine it with the temp path and call Directory.CreateDirectory.

Next: GetRelativePath()

Get the shortest relative path from one folder to a file or folder, using .. steps when needed.

GetRelativePath() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful