41 private static readonly
object systemSynchObject =
new object();
43 private readonly
bool utc;
44 private readonly
string fileNamePattern;
45 private readonly
string placeholderYear;
46 private readonly
string placeholderMonth;
47 private readonly
string placeholderDay;
48 private readonly
string placeholderHour;
49 private readonly
string placeholderMinute;
50 private readonly
string placeholderSecond;
51 private readonly
bool usesYearPlaceholder;
52 private readonly
bool usesMonthPlaceholder;
53 private readonly
bool usesDayPlaceholder;
54 private readonly
bool usesHourPlaceholder;
55 private readonly
bool usesMinutePlaceholder;
56 private readonly
bool usesSecondPlaceholder;
57 private DateTime last = DateTime.MinValue;
58 private bool hasLast =
false;
93 string YearPlaceholder,
string MonthPlaceholder,
string DayPlaceholder,
94 string HourPlaceholder,
string MinutePlaceholder,
string SecondPlaceholder)
95 : this(
FileNamePattern, true, YearPlaceholder, MonthPlaceholder, DayPlaceholder,
96 HourPlaceholder, MinutePlaceholder, SecondPlaceholder)
112 string YearPlaceholder,
string MonthPlaceholder,
string DayPlaceholder,
113 string HourPlaceholder,
string MinutePlaceholder,
string SecondPlaceholder)
118 this.placeholderYear = YearPlaceholder;
119 this.placeholderMonth = MonthPlaceholder;
120 this.placeholderDay = DayPlaceholder;
121 this.placeholderHour = HourPlaceholder;
122 this.placeholderMinute = MinutePlaceholder;
123 this.placeholderSecond = SecondPlaceholder;
125 this.usesYearPlaceholder = !
string.IsNullOrEmpty(this.placeholderYear) && this.fileNamePattern.Contains(this.placeholderYear);
126 this.usesMonthPlaceholder = !
string.IsNullOrEmpty(this.placeholderMonth) && this.fileNamePattern.Contains(this.placeholderMonth);
127 this.usesDayPlaceholder = !
string.IsNullOrEmpty(this.placeholderDay) && this.fileNamePattern.Contains(this.placeholderDay);
128 this.usesHourPlaceholder = !
string.IsNullOrEmpty(this.placeholderHour) && this.fileNamePattern.Contains(this.placeholderHour);
129 this.usesMinutePlaceholder = !
string.IsNullOrEmpty(this.placeholderMinute) && this.fileNamePattern.Contains(this.placeholderMinute);
130 this.usesSecondPlaceholder = !
string.IsNullOrEmpty(this.placeholderSecond) && this.fileNamePattern.Contains(this.placeholderSecond);
136 public bool Utc => this.utc;
206 public DateTime
Last => this.last;
215 if (File.Exists(FileName))
217 int i = FileName.LastIndexOf(
'.');
228 Suffix =
" (" + j.ToString() +
")";
230 FileName2 = FileName.Insert(i, Suffix);
232 while (File.Exists(FileName2) && j < 65536);
237 FileName = FileName2;
251 DateTime TP = this.utc ? DateTime.UtcNow : DateTime.Now;
254 (this.usesSecondPlaceholder && this.last.Second != TP.Second) ||
255 (
this.usesMinutePlaceholder &&
this.last.Minute != TP.Minute) ||
256 (
this.usesHourPlaceholder &&
this.last.Hour != TP.Hour) ||
257 (
this.usesDayPlaceholder &&
this.last.Day != TP.Day) ||
258 (
this.usesMonthPlaceholder &&
this.last.Month != TP.Month) ||
259 (
this.usesYearPlaceholder &&
this.last.Year != TP.Year))
264 FileName = this.fileNamePattern;
266 if (this.usesSecondPlaceholder)
267 FileName = FileName.Replace(this.placeholderSecond, TP.Second.ToString(
"D2"));
269 if (this.usesMinutePlaceholder)
270 FileName = FileName.Replace(this.placeholderMinute, TP.Minute.ToString(
"D2"));
272 if (this.usesHourPlaceholder)
273 FileName = FileName.Replace(this.placeholderHour, TP.Hour.ToString(
"D2"));
275 if (this.usesDayPlaceholder)
276 FileName = FileName.Replace(this.placeholderDay, TP.Day.ToString(
"D2"));
278 if (this.usesMonthPlaceholder)
279 FileName = FileName.Replace(this.placeholderMonth, TP.Month.ToString(
"D2"));
281 if (this.usesYearPlaceholder)
282 FileName = FileName.Replace(this.placeholderYear, TP.Year.ToString(
"D4"));
284 lock (systemSynchObject)
289 StreamWriter fs = File.CreateText(FileName);
Class that generates a sequence of file names, based on the system time.
bool Utc
If UTC timestamps are used.
bool TryGetNewFileName(out string FileName)
Tries to get a new file name.
const string DefaultSecondPlaceholder
SECOND%
string PlaceholderMonth
Placeholder for the current month.
string PlaceholderDay
Placeholder for the current day.
const string DefaultYearPlaceholder
YEAR%
string PlaceholderMinute
Placeholder for the current minute.
static bool MakeUnique(ref string FileName)
Returns a unique file name, if a file with the name already exists.
bool UsesMinutePlaceholder
If the placeholder for the current minute is used.
string PlaceholderHour
Placeholder for the current hour.
FileNameTimeSequence(string FileNamePattern)
Class that generates a sequence of file names, based on the system time.
const string DefaultHourPlaceholder
HOUR%
bool UsesSecondPlaceholder
If the placeholder for the current second is used.
bool UsesDayPlaceholder
If the placeholder for the current day is used.
const string DefaultMonthPlaceholder
MONTH%
bool UsesYearPlaceholder
If the placeholder for the current year is used.
DateTime Last
Last timestamp that generated a new file name.
bool UsesHourPlaceholder
If the placeholder for the current hour is used.
FileNameTimeSequence(string FileNamePattern, bool Utc)
Class that generates a sequence of file names, based on the system time.
FileNameTimeSequence(string FileNamePattern, bool Utc, string YearPlaceholder, string MonthPlaceholder, string DayPlaceholder, string HourPlaceholder, string MinutePlaceholder, string SecondPlaceholder)
Class that generates a sequence of file names, based on the system time.
bool UsesMonthPlaceholder
If the placeholder for the current month is used.
const string DefaultDayPlaceholder
DAY%
string PlaceholderSecond
Placeholder for the current second.
string PlaceholderYear
Placeholder for the current year.
const string DefaultMinutePlaceholder
MINUTE%
string FileNamePattern
File name pattern.
FileNameTimeSequence(string FileNamePattern, string YearPlaceholder, string MonthPlaceholder, string DayPlaceholder, string HourPlaceholder, string MinutePlaceholder, string SecondPlaceholder)
Class that generates a sequence of file names, based on the system time.