C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
However: The .NET Framework offers the excellent Path.GetRandomFileName static parameterless method, which returns appropriate strings.
StaticWriteRandomFile: This method internally gets a random file name, gets the correct output path on the C:\ volume, and then writes some text to it.
Returns: The Path.GetRandomFileName method returns a random file name with an extension. This includes the period before the extension.
ReturnTip: The extension will be 1 dot and 3 letters, while the name will be 8 characters. All the characters are valid file name characters.
C# program that writes random file names
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
m4adstid.dw2
mbhbtcen.5m1
1qtk3r5u.bka
Finally: The char array is converted to a string. This method could probably be significantly optimized.