Microsoft.VsSDK.IntegrationTestLibrary.TestUtils.AddNewItemFromVsTemplate C# (CSharp) Method

AddNewItemFromVsTemplate() public method

Create a new item in the project
public AddNewItemFromVsTemplate ( ProjectItems parent, string templateName, string language, string name ) : ProjectItem
parent ProjectItems the parent collection for the new item
templateName string
language string
name string
return ProjectItem
        public ProjectItem AddNewItemFromVsTemplate(ProjectItems parent, string templateName, string language, string name)
        {
            if (parent == null)
                throw new ArgumentException("project");
            if (name == null)
                throw new ArgumentException("name");

            DTE dte = (DTE)VsIdeTestHostContext.ServiceProvider.GetService(typeof(DTE));

            Solution2 sol = dte.Solution as Solution2;

            string filename = sol.GetProjectItemTemplate(templateName, language);

            parent.AddFromTemplate(filename, name);

            return parent.Item(name);
        }

Usage Example

示例#1
0
        public void VBWinformsApplication()
        {
            UIThreadInvoker.Invoke( (ThreadInvoker) delegate
                                                    {
                                                        //Solution and project creation parameters
                                                        string solutionName = "VBWinApp";
                                                        string projectName = "VBWinApp";

                                                        //Template parameters
                                                        string language = "VisualBasic";
                                                        string projectTemplateName = "WindowsApplication.Zip";
                                                        string itemTemplateName = "CodeFile.zip";
                                                        string newFileName = "Test.vb";

                                                        var dte =
                                                                (DTE)
                                                                VsIdeTestHostContext.ServiceProvider.GetService(
                                                                        typeof (DTE) );

                                                        var testUtils = new TestUtils();

                                                        testUtils.CreateEmptySolution( TestContext.TestDir, solutionName );
                                                        Assert.AreEqual( 0, testUtils.ProjectCount() );

                                                        //Add new  Windows application project to existing solution
                                                        testUtils.CreateProjectFromTemplate( projectName,
                                                                                             projectTemplateName,
                                                                                             language,
                                                                                             false );

                                                        //Verify that the new project has been added to the solution
                                                        Assert.AreEqual( 1, testUtils.ProjectCount() );

                                                        //Get the project
                                                        Project project = dte.Solution.Item( 1 );
                                                        Assert.IsNotNull( project );
                                                        Assert.IsTrue(
                                                                string.Compare( project.Name,
                                                                                projectName,
                                                                                StringComparison.
                                                                                        InvariantCultureIgnoreCase ) ==
                                                                0 );

                                                        //Verify Adding new code file to project
                                                        ProjectItem newCodeFileItem =
                                                                testUtils.AddNewItemFromVsTemplate(
                                                                        project.ProjectItems,
                                                                        itemTemplateName,
                                                                        language,
                                                                        newFileName );
                                                        Assert.IsNotNull( newCodeFileItem,
                                                                          "Could not create new project item" );
                                                    } );
        }