Search code examples
c#fileoutputdatestamp

DateTime stamp in output .zip file


I have a String Path to output a .ZIP file String path = @"C:\TEMP\test.zip"; and I am looking to five the file name a date stamp. Example, test_TodayDate.ZIP. There's a way to achieve this?

Thanks


Solution

  • You can do:

    string filePath = @"C:\TEMP\test.zip";
    string finalPath = Path.Combine(Path.GetDirectoryName(filePath),
                                Path.GetFileNameWithoutExtension(filePath) 
                                      + DateTime.Now.ToString("yyyyMMddHHmmss") 
                                      + Path.GetExtension(filePath));
    
    • First Get File name without extension and add your Time Stamp, Then concatenate file extension,
    • Then get Current directory for the file path
    • Use Path.Combine to combine directory and new file name