Creating a unique file name in C#
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


public static string CreateFileName(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)



to

/// Creates a file in the directory with the given filename
///
/// &lt;param name="AppendNumberIfNameAlreadyExists"&gt;
/// 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.
/// &lt;/param&gt;
public static FileStream CreateUniqueFile(string directory, string filename, bool AppendNumberIfNameAlreadyExists = true)