src/Misc/EFTools/XmlCore/Common/FileUtils.cs
/// <summary>
///     Creates a unique filename based on the users
///     document directory and the input extension
/// </summary>
/// <param name="fileNamePrefix">
///     The file prefix
/// </param>
/// <param name="fileExtension">
///     The file extension.  Should start with a dot.
/// </param>
/// <param name="indexSeparator">
///     If this routine must supply an index then this is the separator.
///     For example '_' will produce myfile_1.txt, myfile_2.txt.  If the
///     separator is string.Empty then myfile1.txt and myfile2.txt will
///     be produced.
/// </param>
/// <param name="alwaysUseSeparator">
///     Flag indicating that separator should always be used even for the
///     first unique name.
/// </param>
/// <param name="tempFileName">
///     the returned temporary filename
/// </param>
Creates a unique filename based on the users
document directory and the input extension
Creates a unique filename based on the users document directory and the input extension
public static void CreateUniqueFilename(
            string fileNamePrefix,
            string fileExtension,
            string indexSeparator,
            bool alwaysUseSeparator,
            out string tempFileName)
        {
            tempFileName = string.Empty;
            if (string.IsNullOrEmpty(fileNamePrefix) == false
                &&
                string.IsNullOrEmpty(fileExtension) == false)
            {
                var filePathAndPrefix = Path.Combine(Path.GetTempPath(), fileNamePrefix);
                var index = alwaysUseSeparator ? 1 : 0;
                while (true)
                {
                    var builder = new StringBuilder(filePathAndPrefix);
                    if (index > 0)
                    {
                        if (string.IsNullOrEmpty(indexSeparator) == false)
                        {
                            builder.Append(indexSeparator);
                        }
                        builder.Append(index.ToString(CultureInfo.InvariantCulture));
                    }
                    builder.Append(fileExtension);
                    tempFileName = builder.ToString();
                    if (File.Exists(tempFileName))
                    {
                        index++;
                    }
                    else
                    {
                        try
                        {
                            using (File.Create(tempFileName))
                            {
                            }
                            break;
                        }
                        catch (IOException)
                        {
                        }
                    }
                    if (index <= 0)
                    {
                        tempFileName = null;
                        break;
                    }
                }
                if (tempFileName == null)
                {
                    tempFileName = Path.GetTempFileName();
                }
            }
        }
public static void CreateUniqueFilename ( string fileNamePrefix , string fileExtension , string indexSeparator , bool alwaysUseSeparator , out string tempFileName ) { tempFileName = string . Empty ; if ( string . IsNullOrEmpty ( fileNamePrefix ) == false && string . IsNullOrEmpty ( fileExtension ) == false ) { var filePathAndPrefix = Path . Combine ( Path . GetTempPath ( ) , fileNamePrefix ) ; var index = alwaysUseSeparator ? 1 : 0 ; while ( true ) { var builder = new StringBuilder ( filePathAndPrefix ) ; if ( index > 0 ) { if ( string . IsNullOrEmpty ( indexSeparator ) == false ) { builder . Append ( indexSeparator ) ; } builder . Append ( index . ToString ( CultureInfo . InvariantCulture ) ) ; } builder . Append ( fileExtension ) ; tempFileName = builder . ToString ( ) ; if ( File . Exists ( tempFileName ) ) { index ++ ; } else { try { using ( File . Create ( tempFileName ) ) { } break ; } catch ( IOException ) { } } if ( index <= 0 ) { tempFileName = null ; break ; } } if ( tempFileName == null ) { tempFileName = Path . GetTempFileName ( ) ; } } }
Creates a unique filename based on the users
document directory and the input extension
Creates a unique filename based on the users document directory and the input extension
// There's no routine in C# to return a temp file with a // specified extension and directory.  So we build it up here. // 0 means don't use separator if possible. // This may happen if the file got created by something else between the time File.Exists and File.Create. // The integer wrapped around.  We need to return an empty // string because we were not able to create the file. // fallback to the standard method that disregards the template
fileNamePrefix
string
The file prefix
The file prefix
fileExtension
string
The file extension.  Should start with a dot.
The file extension . Should start with a dot .
indexSeparator
string
If this routine must supply an index then this is the separator.
For example '_' will produce myfile_1.txt, myfile_2.txt.  If the
separator is string.Empty then myfile1.txt and myfile2.txt will
be produced.
If this routine must supply an index then this is the separator . For example ' _ ' will produce myfile_1 . txt myfile_2 . txt . If the separator is string . Empty then myfile1 . txt and myfile2 . txt will be produced .
alwaysUseSeparator
bool
Flag indicating that separator should always be used even for the
first unique name.
Flag indicating that separator should always be used even for the first unique name .
string
out
tempFileName
the returned temporary filename
the returned temporary filename