how to create folder and file with datetime in wpf application

299 views Asked by At

I have WPF application. where I want to create folder within folder with datetime format. I tried below code

string reportPath= environment.currentDirectory+"\\Reports\\";
string datetime= Datetime.now.tostring("dd-MM-yyyy_HH:mm:tt");
string todaysDateFolder= path.combine(reportPath,datetime);


//string todaysDateFolder = reportPath+datetime+"\\";  //This opetion also try but get error 

code to create directory folderlike as below

 if (!Directory.Exist(reportPath))
  {
      Directory.createDirectory(reportpath);
      Directory.createDirectory(todaysDateFolder);
  } 
  else
    {
        Directory.createDirectory(todaysDateFolder); ///get error here that path format is not supported     
     }

if I use any fix name instead of date , It works. But I want date formatted folder also file.

1

There are 1 answers

0
Sheridan On BEST ANSWER

When using a date field in a folder or file name, it is customary to use this reverse format, so that they can be ordered correctly:

string formattedDate = Datetime.Now.ToString("yyyyMMddHHmmssfff");

If you format using the day first, then the folders will not be able to be ordered by date. This format also has no forbidden characters in it (like the colons (:) in your date format. You can find out which characters cannot be used in filenames in the Naming Files, Paths, and Namespaces page on MSDN.