How to put timestamp on file while creating unique file name using class file?

In this article, I have added code for timestamp creation. Whenever any particular event occurs, it can be recorded by using timestamp. Logging events become possible with the help of timestamp. This functionality can be placed inside common functionality folder and use throughout of your application.

Description - With the help of time stamp, file can be identified with uniqueness. Create TimeStampCreation.cs class file and put following lines of code into it.
Latest file can be tracked by using timestamp.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleApp
public static class TimeStampCreation

public static string GetDateTimeStampString(string sourcefile, bool flagfile)
string strtimeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
string strfinalstring = sourcefile + strtimeStamp;
if (sourcefile.IndexOf('.') > 0)
strfinalstring = sourcefile.Insert(sourcefile.IndexOf('.'), "_" + strtimeStamp);
if (!flagfile)
strfinalstring = strfinalstring.Split('.')[0];

return strfinalstring;

You can use any dateformat as per the requirement inside DateTime.Now.ToString("put your date time format here")


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: