Append TimeStamp to a File Name

后端 未结 6 762
清歌不尽
清歌不尽 2020-12-07 16:57

I have come across this problem several times in which I would like to have multiple versions of the same file in the same directory. The way I have been doing it using C# i

相关标签:
6条回答
  • 2020-12-07 17:36

    For Current date and time as the name for a file on the file system. Now call the string.Format method, and combine it with DateTime.Now, for a method that outputs the correct string based on the date and time.

    using System;
    using System.IO;
    
    class Program
    {
        static void Main()
        {
            //
            // Write file containing the date with BIN extension
            //
            string n = string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin",
                DateTime.Now);
            File.WriteAllText(n, "abc");
        }
    }
    

    Output :

    C:\Users\Fez\Documents\text-2020-01-08_05-23-13-PM.bin
    

    "text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin"

    text- The first part of the output required Files will all start with text-

    {0: Indicates that this is a string placeholder The zero indicates the index of the parameters inserted here

    yyyy- Prints the year in four digits followed by a dash This has a "year 10000" problem

    MM- Prints the month in two digits

    dd_ Prints the day in two digits followed by an underscore

    hh- Prints the hour in two digits

    mm- Prints the minute, also in two digits

    ss- As expected, it prints the seconds

    tt Prints AM or PM depending on the time of day

    | |
    0 讨论(0)
  • 2020-12-07 17:37

    Perhaps appending DateTime.Now.Ticks instead, is a tiny bit faster since you won't be creating 3 strings and the ticks value will always be unique also.

    0 讨论(0)
  • 2020-12-07 17:48

    You can use below instead:

    DateTime.Now.Ticks
    
    0 讨论(0)
  • 2020-12-07 17:52

    You can use DateTime.ToString Method (String)

    DateTime.Now.ToString("yyyyMMddHHmmssfff")

    or string.Format

    string.Format("{0:yyyy-MM-dd_HH-mm-ss-fff}", DateTime.Now);

    or Interpolated Strings

    $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss-fff}"

    There are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).

    With Extension Method

    Usage:

    string result = "myfile.txt".AppendTimeStamp();
    //myfile20130604234625642.txt
    

    Extension method

    public static class MyExtensions
    {
        public static string AppendTimeStamp(this string fileName)
        {
            return string.Concat(
                Path.GetFileNameWithoutExtension(fileName),
                DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                Path.GetExtension(fileName)
                );
        }
    }
    
    0 讨论(0)
  • 2020-12-07 17:55

    you can use:

    Stopwatch.GetTimestamp();
    
    0 讨论(0)
  • 2020-12-07 18:02

    I prefer to use:

    string result = "myFile_" + DateTime.Now.ToFileTime() + ".txt";
    

    What does ToFileTime() do?

    Converts the value of the current DateTime object to a Windows file time.

    public long ToFileTime()

    A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC). Windows uses a file time to record when an application creates, accesses, or writes to a file.

    Source: MSDN documentation - DateTime.ToFileTime Method

    0 讨论(0)
提交回复
热议问题