.NET Framework Class Library
Path..::.GetRandomFileName Method

Returns a random folder name or file name.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Shared Function GetRandomFileName As String
Visual Basic (Usage)
Dim returnValue As String

returnValue = Path.GetRandomFileName()
C#
public static string GetRandomFileName()
Visual C++
public:
static String^ GetRandomFileName()
JScript
public static function GetRandomFileName() : String

Return Value

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

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.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Community Content

Patrick McDonald
GetRandomFileName with PowerShell
# get-randomfilename.ps1
# Gets a random filename
# Thomas Lee - tfl@psp.co.uk

# Get two random file names:
$fn1 = [system.io.path]::getrandomfilename()
$fn2 = [system.io.path]::getrandomfilename()

# Display Results
"Random file name 1: {0}" -f $fn1
"Random file name 2: {0}" -f $fn2A

This script produces the following output (NB: file names produced varies each time the script is run!)

PSH [C:\foo]: C:\foo\get-randomfilename.ps1
Random file name 1: jfy0j0gs.abh
Random file name 2: g4hpusfo.lbd



fatcat1111
Output is 8.3
The documentation doesn't mention this, but the output of this method conforms to the old DOS "8.3" naming conventions. That is, the file name is 8 chars long, and the extension is 3 chars long.

j153c
Documentation bug: GetRandomFilename method has nothing to do with the security
'When the security of your file system is paramount, this method should be used instead of GetTempFileName.'

The statement above ought to be removed from MSDN documentation. The GetRandomFilename method has nothing to do with the security of your file system, and promoting so would not only be bad practice but is insecure and ultimately wrong.

-jc

Page view tracker