Skip to content

Instantly share code, notes, and snippets.

@yooakim
Created February 1, 2016 11:19
Show Gist options
  • Save yooakim/a60a1351432cf84ceb8a to your computer and use it in GitHub Desktop.
Save yooakim/a60a1351432cf84ceb8a to your computer and use it in GitHub Desktop.
Add timestamp to filename
public static class Extensions
{
/// <summary>
/// Add a timestamp to the filename, before the extension.
/// </summary>
/// <param name="fileName">any filename for which you want to add a timestamp, including file extension.</param>
/// <returns>filename with added timestamp</returns>
/// <example>
/// <c>var file = "myfile.txt".AddTimeStamp();</c>
/// This would return the filename "myfile__20160201120840492.txt"
/// </example>
public static string AddTimeStamp(this string fileName)
{
return string.Concat(
Path.GetFileNameWithoutExtension(fileName),
DateTime.Now.ToString("_yyyyMMddHHmmssfff"),
Path.GetExtension(fileName)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment