3 approaches to generate unique filename in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 51989 red flag

This article will show three different approach to generate unique file name in C#

3 approaches to generate unique filename in C#

In this article we will show three different approaches to generate random file name In C#.  File uploading and handling is very common scenario in project development. And it’s always good practice to change file name after uploading by user.

So , in this situation we have to change file name programmatically and obviously we have to generate unique file name to do that. There are various approach to do that , we will see one by one each of them.

Using GUID

This is most recommended way to generate unique number in C#. We can use same technique to generate file name too.  GUID will generate 32 bit character combination as a unique string. Generally it’s enough unique to use as file name. And it’s very fast way to generate unique string from other.

In below code I have given example code of GUID .

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Test1
{
    class Program
    {
        static void Main(string[]args)
        {
            string FileName = string.Format(@"{0}.pdf", Guid.NewGuid());
            Console.WriteLine(FileName);
            Console.ReadLine();
        }
    }
}

And the output screen is here.


 Using GetRandomFileName() function

This is one function to generate random file name in c# program. This function will generate file name with few random character and the number of character not fixed. Basically it returns cryptographically strong

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = System.IO.Path.GetRandomFileName();
            Console.WriteLine(FileName);
            Console.ReadLine();
        }
    }
}
 


Combination of GUID and DateTime

This is another way to generate unique file name. Those who are not trust guid enough they implement combination of both guid and timestamp. We know timestamp is nothing but combination of current time with hour ,minute ,second even millisecond . And if we concatenate this combination with GUID random string then I hope always it will throw a unique string even in strong multi threading environment. In below I have given sample code to implement this.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName =  "Image"+ "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString();
            Console.WriteLine(FileName);
 
            Console.ReadLine();
        }
    }
}
 

  The output screen is in below


conclusion :

 Those are the three different approach to generate file name in C# . If you find another please write in comment


Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)