Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Path.GetRandomFileName Method

Returns a random folder name or file name.

Namespace:  System.IO
Assemblies:   mscorlib (in mscorlib.dll)
  System.Runtime.Extensions (in System.Runtime.Extensions.dll)

public static string GetRandomFileName()

Return Value

Type: System.String
A random folder name or file name.

The GetRandomFileName method returns a cryptographically strong, random string that can be used as either a folder name or a file name. Unlike GetTempFileName, GetRandomFileName does not create a file. When the security of your file system is paramount, this method should be used instead of GetTempFileName.

The following example show output from the GetRandomFileName method.

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string result = Path.GetRandomFileName();
            Console.WriteLine("Random file name is " + result);
        }
    }
}

/*

 This code produces output similar to the following:

 Random file name is w143kxnu.idj
 Press any key to continue . . .

 */

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft