1

I want to create a Filename with DateTime.Now to store the errors that catched using Exception Handling everyday.

I used DateTime.ToFileTime, but the format appending for not in date format.

string result = "myFile_" + DateTime.Now.ToFileTime() + ".txt";
string path = "E:\\ErrorCollector\\ErrorCollector" + DateTime.Now.ToFileTime()+ ".txt";

FileStream fi = new FileStream(path, FileMode.OpenOrCreate);
StreamWriter sw1 = new StreamWriter(fi);
sw1.WriteLine(DateTime.Now + "" + ex.message);

I am Expecting the filename like "ErrorCollector17/08/2019"

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Ashwin Kumar
  • 61
  • 1
  • 11
  • 1
    What format do you want to get exactly? How about using custom formatting instead? – Soner Gönül Aug 08 '19 at 06:03
  • 1
    Possible duplicate of https://stackoverflow.com/questions/12500091/datetime-tostring-format-that-can-be-used-in-a-filename-or-extension – Soundararajan Aug 08 '19 at 06:07
  • If i use the above code, My file is created like "ErrorCollector132097177851536974" , I wish it should be created with (dd/MM/yyyy) format. – Ashwin Kumar Aug 08 '19 at 06:09
  • @AshwinKumar This is _how_ `ToFileTime` works. It would be better to read it's documentation before using any functionality when you writing code. In your case, you can use `DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)` for example to generate a custom formatted string. https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – Soner Gönül Aug 08 '19 at 06:12
  • 1
    As another suggestion, yyyyMMdd format is generally preferred because if you sort the the files in a folder by name, the most recent one is on the top. – Barış Akkurt Aug 08 '19 at 06:15

3 Answers3

3

You are not allowed to create filename which contains any of following characters: /:*?"<>| on Windows, you can do like this

string path = "E:\\ErrorCollector\\ErrorCollector" + DateTime.Now.ToString("dd-MM-yyyy")+ ".txt"
Sonduong
  • 56
  • 1
2

You can try to use ToString function with a format.

 DateTime.Now.ToString("dd/MM/yyyy",new System.Globalization.CultureInfo("en-US"));

c# online

As Soundararajan say I would suggest you use

"ddMMyyyy"

or

"dd-MM-yyyy"

due to the system will confuse your path contain \

D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • 1
    / is a path separating character in unix based systems (Linux / Mac). So it might not be a good option to have as part of filename. Suggest that you replace it with an allowed character for filenames (e.x -) – Soundararajan Aug 08 '19 at 06:06
  • 1
    ThankYou Sir, Its Working. – Ashwin Kumar Aug 08 '19 at 06:14
1

shortest answer would be the code below:

DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);

DateTime.Now gets the current date and time based on your computer's clock.

.ToString(...) converts the DateTime object into a string with optional formatting inside as parameters.

"yyyyMMddHHmmss" is a pattern for how you want the DateTime object be mapped in a string manner where. assuming your computer's clock is currently ticked at "August 8, 2019 12:34:56 PM", then:

  • yyyy = is a 4 digit year as 2019
  • MM = is a 2 digit month equivalent such as 08
  • dd = is a 2 digit day of the year such as 08
  • HH = is a 2 digit hour equivalent in 24 Hours format such as 12
  • mm = is a 2 digit minute such as 34
  • ss = is a 2 digit second such as 56

and the output would be 20190808123456. Note that the arrangement of year, month, date, hour, minute, or even second can be in no specific order.

CultureInfo.InvariantCulture is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings (via source)

note that we removed special characters separating different parts of the DateTime object to prevent issues when filenames on Windows.

Nii
  • 450
  • 6
  • 25