Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
Path Class
Path Methods
 GetRandomFileName Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Path..::.GetRandomFileName Method

Returns a random folder name or file name.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
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.

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.

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
GetRandomFileName with PowerShell      Thomas Lee ... Patrick McDonald   |   Edit   |   Show History
# 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


Output is 8.3      fatcat1111   |   Edit   |   Show History
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.
Documentation bug: GetRandomFilename method has nothing to do with the security      j153c   |   Edit   |   Show History
'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
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker