如何以编程方式将文件添加到Visual Studio模板?

Man*_*nis 5 visual-studio visual-studio-templates vsix visual-studio-2017

我已经为.net核心应用程序创建了Visual Studio项目模板。使用此模板创建新项目时,我希望能够在Azure身份验证和身份验证之间进行选择。为了实现这一点,我创建了一个向导。根据向导上选择的选项,项目中必须包含一些其他文件。我的问题是我不知道如何将这些额外的文件添加到我的项目中。

第一种方法

首先,我创建了一个项目模板,并尝试将其内容添加到我的项目中。

AzureAuthentication.vs模板

<VSTemplate>
    <TemplateData>
        <!-- code removed for clarity -->
    </TemplateData>
    <TemplateContent>
        <Folder Name="Models" TargetFolderName="Models">
            <ProjectItem ReplaceParameters="true" TargetFileName="RefreshTokenResponse.cs">RefreshTokenResponse.cs</ProjectItem>
        </Folder>
        <Folder Name="Controllers" TargetFolderName="Controllers">
            <ProjectItem ReplaceParameters="true" TargetFileName="UserController.cs">UserController.cs</ProjectItem>
        </Folder>
    </TemplateContent>
</VSTemplate>
Run Code Online (Sandbox Code Playgroud)

WizardImplementation.cs

public class WizardImplementation : IWizard {
    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) {
        dte = automationObject as DTE;

        // code removed for clarity
    }

    public void ProjectFinishedGenerating(Project project) {
        switch (authenticationType) {
            default: break;
            case AuthenticationType.Azure: {
                    // code removed for clarity

                    using (StreamWriter writer = File.CreateText("C:/Users/pasjiannis.OFFICE/Desktop/log.txt")) {
                        dte.Solution.AddFromTemplate(templatePath, projectPath, "AzureAuthentication");
                    }

                    break;
                }
            case AuthenticationType.Identity: {
                    break;
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

这种方法根本行不通。

第二种方法

然后,我将模板从项目模板转换为项目模板。

AzureAuthentication.vs模板

<VSTemplate>
    <TemplateData>
        <!-- code removed for clarity -->
    </TemplateData>
    <TemplateContent>
        <Project File="AzureAuthentication.csproj" ReplaceParameters="true">
            <Folder Name="Models" TargetFolderName="Models">
                <ProjectItem ReplaceParameters="true" TargetFileName="RefreshTokenResponse.cs">RefreshTokenResponse.cs</ProjectItem>
            </Folder>
            <Folder Name="Controllers" TargetFolderName="Controllers">
                <ProjectItem ReplaceParameters="true" TargetFileName="UserController.cs">UserController.cs</ProjectItem>
            </Folder>
        </Project>
    </TemplateContent>
</VSTemplate>
Run Code Online (Sandbox Code Playgroud)

这部分起作用。这些文件已成功添加到我的项目中,但是另一个项目也已创建到我的解决方案中。

第三种方法

我试着访问我的项目,而不是在WizardImplementation类中使用AddFromTemplate方法,获取其项目项并在其中添加新项

dte.Solution.Projects.Items(0).ProjectItems.AddFromFile("UserController.cs");
Run Code Online (Sandbox Code Playgroud)

但是我的专案上的ProjectItems为null。

我在这里想念什么?我的方法是否正确或有更好的方法来实现这一目标?

Man*_*nis 0

我用第三种方法解决了这个问题。我错过了一个事实,即为dte.Solution.Projects.Items(0)我提供了包含我的项目的文件夹,而不是项目本身。

我所要做的就是改变:

dte.Solution.Projects.Items(0).ProjectItems.AddFromFile("UserController.cs");

dte.Solution.Projects.Items(0).SubProject.ProjectItems.AddFromFile("UserController.cs");