patterncsharpModerate
Creating a unique file name in C#
Viewed 0 times
creatinguniquefilename
Problem
I find that I am often writing files where the names either:
And, so, I've created the following methods to do so:
```
public static string CreateDatedFileName(string directory, string filename, string DateTimeFormat = "yyyy-MM-dd", bool AppendNumberIfNameAlreadyExists = true)
{
if (!CheckIfDirectoryExists(directory))
throw new ArgumentException(string.Format("The specified directory - {0} - does not exist", directory));
string formatedTemplate = "{0} - {1}.{2}";
var fileNameWithoutExtension = filename.Split('.')[0];
var fileExtension = filename.Split('.')[1];
var DateValue = DateTime.Now.ToString(DateTimeFormat);
string newname = String.Format(formatedTemplate, fileNameWithoutExtension, DateValue, fileExtension);
string path = Path.Combine(directory, newname);
if (!AppendNumberIfNameAlreadyExists)
return Path.Combine(directory, newname);
else
return CreateFileName(directory, newname, true);
}
public static string CreateFileName(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)
{
if (!CheckIfDirectoryExists(directory))
throw new ArgumentException(string.Format("The specified directory - {0} - does not exist", directory));
string formatedTemplate = "{0}{1}.{2}";
var fileNameWithoutExtension = filename.Split('.')[0];
var fileExtension = filename.Split('.')[1];
string newname = String.Format(formatedTemplate, fileNameWithoutExtension, string.Empty, fileExtension);
string path = Path.Combine(directory, newname);
int AppendDigit = 0;
while (AppendNumberIfNameAlreadyExists && CheckIfFilePathExists(path))
{
++AppendDigit;
newname = String.Format(formatedTemplate, fileNameWithoutExtension, "_" + AppendDigit, fileExtension);
path = Path.Combine(directory, newname);
}
- Need to be incremented (rather than overwriting a file that already exists in the directory with the same name)
- Need to be timestamped
And, so, I've created the following methods to do so:
```
public static string CreateDatedFileName(string directory, string filename, string DateTimeFormat = "yyyy-MM-dd", bool AppendNumberIfNameAlreadyExists = true)
{
if (!CheckIfDirectoryExists(directory))
throw new ArgumentException(string.Format("The specified directory - {0} - does not exist", directory));
string formatedTemplate = "{0} - {1}.{2}";
var fileNameWithoutExtension = filename.Split('.')[0];
var fileExtension = filename.Split('.')[1];
var DateValue = DateTime.Now.ToString(DateTimeFormat);
string newname = String.Format(formatedTemplate, fileNameWithoutExtension, DateValue, fileExtension);
string path = Path.Combine(directory, newname);
if (!AppendNumberIfNameAlreadyExists)
return Path.Combine(directory, newname);
else
return CreateFileName(directory, newname, true);
}
public static string CreateFileName(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)
{
if (!CheckIfDirectoryExists(directory))
throw new ArgumentException(string.Format("The specified directory - {0} - does not exist", directory));
string formatedTemplate = "{0}{1}.{2}";
var fileNameWithoutExtension = filename.Split('.')[0];
var fileExtension = filename.Split('.')[1];
string newname = String.Format(formatedTemplate, fileNameWithoutExtension, string.Empty, fileExtension);
string path = Path.Combine(directory, newname);
int AppendDigit = 0;
while (AppendNumberIfNameAlreadyExists && CheckIfFilePathExists(path))
{
++AppendDigit;
newname = String.Format(formatedTemplate, fileNameWithoutExtension, "_" + AppendDigit, fileExtension);
path = Path.Combine(directory, newname);
}
Solution
The only way to guarantee that the generated filename is truly unique is to actually create the file. Any other method is vulnerable to a race condition. Therefore, if you are serious about uniqueness, you should change
to
public static string CreateFileName(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)to
/// Creates a file in the directory with the given filename
///
///
/// If true, then a numbered suffix (starting from 0) is appended
/// to the filename if necessary to avoid a name clash.
///
/// If false, then an IOException is thrown if the file already exists.
///
public static FileStream CreateUniqueFile(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)Code Snippets
public static string CreateFileName(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)/// Creates a file in the directory with the given filename
///
/// <param name="AppendNumberIfNameAlreadyExists">
/// If true, then a numbered suffix (starting from 0) is appended
/// to the filename if necessary to avoid a name clash.
///
/// If false, then an IOException is thrown if the file already exists.
/// </param>
public static FileStream CreateUniqueFile(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)Context
StackExchange Code Review Q#116709, answer score: 11
Revisions (0)
No revisions yet.