Thinktecture.Tools.Web.Services.CodeGeneration.CodeWriter.GetUniqueFileName C# (CSharp) Method

GetUniqueFileName() private static method

This is a helper method for acquiring a unique file name.
private static GetUniqueFileName ( string directory, string fileName, CodeLanguage language, bool overwrite ) : string
directory string
fileName string
language CodeLanguage
overwrite bool
return string
        private static string GetUniqueFileName(string directory, string fileName, CodeLanguage language, bool overwrite)
        {
            // Get the appropriate file extension for the selected programming language.
            string ext = CodeWriter.GetExtension(language);
            // Read the file name without the extension.
            string fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
            // Construct the absolute path of the file by concatanating directory name, file name and the
            // extension.
            string absPath = Path.Combine(directory, string.Format("{0}.{1}", fileNameWithoutExt, ext));

            // Can't we overwrite files?
            if (!overwrite)
            {
                // We arrive here if we cannot overwrite existing files.

                // Counter used for generating the numeric constant for unique file name generation.
                int counter = 1;

                // Create a new file name until the created file name does not exist in the file system.
                while (File.Exists(absPath))
                {
                    // Create the new file name.
                    absPath = Path.Combine(directory, string.Format("{0}{1}.{2}", fileNameWithoutExt, counter.ToString(), ext));
                    // Increment the counter.
                    counter++;
                }
            }

            // Finally return the generated absolute path of the file.
            return absPath;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// This method writes each type generated into a separate file.
        /// The type name is used as the file name.
        /// </summary>
        private void WriteSeparateCodeFiles()
        {
            // Some assertions to make debugging easier.
            Debug.Assert(!string.IsNullOrEmpty(options.OutputLocation), "This action cannot be performed when output location is null or an empty string.");

            codeFilesCount = codeNamespace.Types.Count;
            // Initialize the file names array to hold enough all our type names.
            generatedCodeFileNames = new string[codeFilesCount + textFiles.Count];

            // Itterate the the CodeTypeDeclaration types in the codeNamespace and
            // generate a file for each of them.
            for (int i = 0; i < codeNamespace.Types.Count; i++)
            {
                // Take a reference to the CodeTypeDeclaration at the current index.
                CodeTypeDeclaration ctd = codeNamespace.Types[i];
                // Create a temporary CodeNamespace.
                CodeNamespace tempns = new CodeNamespace(codeNamespace.Name);
                // Add the type to the temporary namespace.
                tempns.Types.Add(ctd);
                // Get the destination file name.
                string fileName = CodeWriter.GetUniqueFileName(options.OutputLocation, ctd.Name, options.Language, options.OverwriteExistingFiles);
                // Create a StreamWriter for writing to the destination file.
                StreamWriter writer = new StreamWriter(fileName);
                try
                {
                    // Write out the code to the destination file.
                    provider.GenerateCodeFromNamespace(tempns, writer, codeGenerationOptions);
                    // Flush all buffers in the writer.
                    writer.Flush();
                    // Finally add the file name to the generated file names array.
                    generatedCodeFileNames[i] = fileName;
                }
                catch (IOException e)
                {
                    // Wrap the IOException in a CodeWriterException with little bit
                    // more information.
                    throw new CodeWriterException(
                              string.Format("An error occurred while trying write to file {0}: {1}", fileName, e.Message), e);
                }
                finally
                {
                    // No matter what happens, dispose the stream writer and release the unmanaged
                    // resources.
                    writer.Dispose();
                }
            }
        }
All Usage Examples Of Thinktecture.Tools.Web.Services.CodeGeneration.CodeWriter::GetUniqueFileName