You are hereC# Tips and Trick / File Management

File Management


By admin - Posted on 10 June 2010

This code snip will create a full path filename in one of the system environment folders and will provide an optional time-date stamp that will sort in time order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static string GenerateFilename(Environment.SpecialFolder folder, string fileName, string fileExtention, bool includeDateTimeStamp)
{
 
    if (!includeDateTimeStamp)
    {
        return Path.Combine(
          Environment.GetFolderPath(folder), 
          string.Format("{0}.{1}", fileName, fileExtention));
    }
    else
    {
       return Path.Combine(Environment.GetFolderPath(folder), 
          string.Format("{0}_{1}.{2}",
          fileName, 
          DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH'_'mm'_'ss"), 
          fileExtention));
 
    }
 
}