src/T4Toolbox.VisualStudio/OutputFileManager.cs
/// <summary>
/// Adds a folder to a specified <paramref name="collection" /> of project items.
/// </summary>
/// <param name="collection">
/// A <see cref="ProjectItems" /> collection that belongs to a <see cref="Project" /> or
/// <see cref="ProjectItem" /> of type <see cref="EnvDTE.Constants.vsProjectItemKindPhysicalFolder" />.
/// </param>
/// <param name="folderName">
/// Name of the folder to be added.
/// </param>
/// <param name="basePath">
/// Absolute path to the directory where the folder is located.
/// </param>
/// <returns>
/// A <see cref="ProjectItem" /> that represents new folder added to the <see cref="Project" />.
/// </returns>
/// <remarks>
/// If the specified folder doesn't exist in the solution and the file system,
/// a new folder will be created in both. However, if the specified folder
/// already exists in the file system, it will be added to the solution instead.
/// Unfortunately, an existing folder can only be added to the solution with
/// all of sub-folders and files in it. Thus, if a single output file is
/// generated in an existing folders not in the solution, the target folder will
/// be added to the solution with all files in it, generated or not. The
/// only way to avoid this would be to immediately remove all child items
/// from a newly added existing folder. However, this could lead to having
/// orphaned files that were added to source control and later excluded from
/// the project. We may need to revisit this code and access <see cref="SourceControl" />
/// automation model to remove the child items from source control too.
/// </remarks>
Adds a folder to a specified  of project items.
Adds a folder to a specified of project items .
private static ProjectItem AddFolder(ProjectItems collection, string folderName, string basePath)
        {
            ProjectItem folder = collection.Cast<ProjectItem>().FirstOrDefault(p => string.Equals(p.Name, folderName, StringComparison.OrdinalIgnoreCase));
            if (folder != null)
            {
                return folder;
            }
            try
            {
                return collection.AddFolder(folderName);
            }
            catch (COMException)
            {
                string folderPath = Path.Combine(basePath, folderName);
                if (Directory.Exists(folderPath))
                {
                    return collection.AddFromDirectory(folderPath);
                }
                throw;
            }
        }
private static ProjectItem AddFolder ( ProjectItems collection , string folderName , string basePath ) { ProjectItem folder = collection . Cast < ProjectItem > ( ) . FirstOrDefault ( p => string . Equals ( p . Name , folderName , StringComparison . OrdinalIgnoreCase ) ) ; if ( folder != null ) { return folder ; } try { return collection . AddFolder ( folderName ) ; } catch ( COMException ) { string folderPath = Path . Combine ( basePath , folderName ) ; if ( Directory . Exists ( folderPath ) ) { return collection . AddFromDirectory ( folderPath ) ; } throw ; } }
Adds a folder to a specified  of project items.
Adds a folder to a specified of project items .
// Does the folder already exist in the solution? // Try adding folder to the project. // Note that this will work for existing folder in a Database project but not in C#. // If folder already exists on disk and the previous attempt to add failed // Try adding it from disk // Note that this will work in a C# but is not implemented in Database projects.
A  that represents new folder added to the .
A that represents new folder added to the .
collection
ProjectItems
A  collection that belongs to a  or
of type .
A collection that belongs to a or of type .
folderName
string
Name of the folder to be added.
Name of the folder to be added .
basePath
string
Absolute path to the directory where the folder is located.
Absolute path to the directory where the folder is located .
remarks
If the specified folder doesn't exist in the solution and the file system,
a new folder will be created in both. However, if the specified folder
already exists in the file system, it will be added to the solution instead.
Unfortunately, an existing folder can only be added to the solution with
all of sub-folders and files in it. Thus, if a single output file is
generated in an existing folders not in the solution, the target folder will
be added to the solution with all files in it, generated or not. The
only way to avoid this would be to immediately remove all child items
from a newly added existing folder. However, this could lead to having
orphaned files that were added to source control and later excluded from
the project. We may need to revisit this code and access
automation model to remove the child items from source control too.
If the specified folder doesn ' t exist in the solution and the file system a new folder will be created in both . However if the specified folder already exists in the file system it will be added to the solution instead . Unfortunately an existing folder can only be added to the solution with all of sub - folders and files in it . Thus if a single output file is generated in an existing folders not in the solution the target folder will be added to the solution with all files in it generated or not . The only way to avoid this would be to immediately remove all child items from a newly added existing folder . However this could lead to having orphaned files that were added to source control and later excluded from the project . We may need to revisit this code and access automation model to remove the child items from source control too .