Path.GetTempFileName()
to generate unique filenames rather than relying on DateTime
CS-S1004One way to generate unique files is to rely on DateTime.Now.Ticks
and then append this filename to the temp path. However, .NET provides APIs to generate reliable temp files. You can combine Path.GetTempPath()
and Path.GetTempFileName()
to get the full path to a uniquely generated file that is comparatively more secure and reliable.
var temp = Path.Combine(Path.GetTempPath(), DateTime.Now.Ticks + ".tmp");
var temp = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());