How to generate random file name in C#

3 Answers

0 votes
using System;
using System.IO;

// random name with an extension

public class Program
{
    public static void Main(string[] args)
    {
        string fileName = Path.GetRandomFileName();
 
        Console.WriteLine(fileName);
    }
}



/*
run:
    
ox2y2dto.vqf

*/

 



answered Feb 15, 2017 by avibootz
edited 22 hours ago by avibootz
0 votes
using System;
using System.IO;

// random name without an extension

public class Program
{
    public static void Main(string[] args)
    {
        string fileName = Path.GetRandomFileName().Replace(".", "");
        
        Console.WriteLine(fileName);
    }
}


/*
run:

n8aqfpodh90

*/

 



answered 22 hours ago by avibootz
0 votes
using System;

// unique filename with a specific extension
 
public class Program
{
    public static void Main(string[] args)
    {
        string fileName = $"{Guid.NewGuid()}.txt";
         
        Console.WriteLine(fileName);
    }
}
 
 
/*
run:
 
45380e1e-0bb3-4e12-a57b-e82e0f491981.txt
 
*/

 



answered 22 hours ago by avibootz
edited 22 hours ago by avibootz

Related questions

1 answer 175 views
1 answer 89 views
89 views asked Nov 6, 2022 by avibootz
1 answer 239 views
1 answer 125 views
1 answer 126 views
...