Visual Studio 可扩展性,以编程方式创建项目

发布于 2024-07-17 05:26:21 字数 2699 浏览 8 评论 0原文

我正在尝试以编程方式将测试项目添加到解决方案中。 但是,当执行下面的代码时,我在“vhaSolution.GetProjectTemplate("TestProject.zip", "Csharp")”行上收到文件 IO 异常。 该错误表明“任何已安装的软件包都不支持指定的语言”。 有谁知道这可能是什么原因造成的?

   public enum TestProjectType
    {
        Unit,
        Acceptance,
        Integration
    }

 public static void CreateTestProject(string fullyQualifiedSolutionFileName,string projectName,TestProjectType testProjectType)
        {
            #region Argument Validation
            if (String.IsNullOrEmpty(fullyQualifiedSolutionFileName) || String.IsNullOrEmpty(fullyQualifiedSolutionFileName.Trim()))
            {
                throw new ArgumentNullException("fullyQualifiedSolutionFileName", "The solution file location is required.");
            }

            if (String.IsNullOrEmpty(projectName) || String.IsNullOrEmpty(projectName.Trim()))
            {
                throw new ArgumentNullException("projectName", "The project name is required.");
            }

            if (!File.Exists(fullyQualifiedSolutionFileName))
            {
                throw new ArgumentException(String.Format("The file {0} specified does not exist.", fullyQualifiedSolutionFileName));
            }

            if (testProjectType == null) testProjectType = TestProjectType.Unit;
            #endregion

            System.Type vsType = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0");
            Object vs = System.Activator.CreateInstance(vsType, true);
            EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)vs;

            Solution2 vhaSolution = (Solution2)dte8Obj.Solution;
            vhaSolution.Open(fullyQualifiedSolutionFileName);

            //TODO: Externalize company name
            string cmpnyName = "Vha";
            string testProjectName = String.Format("{0}.{1}.{2}{3}",cmpnyName,projectName,testProjectType.ToString(),"Test");
            string testTemplateLocation = vhaSolution.GetProjectTemplate("TestProject.zip", "CSharp");
            FileInfo rootSolutionFolder = new FileInfo(fullyQualifiedSolutionFileName);

            //TODO: Externalize test directory name
            string testDirName = String.Format("{0}\\{1}\\{2}\\{3}",rootSolutionFolder.DirectoryName,"test",testProjectType.ToString(),testProjectName);

            if (!Directory.Exists(testDirName))
            {
                //may throw an exception if the dir can't be created...
                Directory.CreateDirectory(testDirName);
            }


            Project vhaTestProj = vhaSolution.AddFromTemplate(testTemplateLocation,testDirName,testProjectName + ".proj",false);
            vhaTestProj.Save(String.Format("{0}\\{1}.proj",testDirName , testProjectName));



        }

I am attempting to programmatically add a test project to a solution. However when the code below executes I receive a File IO exception on the line "vhaSolution.GetProjectTemplate("TestProject.zip", "Csharp")". The error indicates that "he language specified is not supported by any of the installed packages". Does anyone have any idea what could be causing this?

   public enum TestProjectType
    {
        Unit,
        Acceptance,
        Integration
    }

 public static void CreateTestProject(string fullyQualifiedSolutionFileName,string projectName,TestProjectType testProjectType)
        {
            #region Argument Validation
            if (String.IsNullOrEmpty(fullyQualifiedSolutionFileName) || String.IsNullOrEmpty(fullyQualifiedSolutionFileName.Trim()))
            {
                throw new ArgumentNullException("fullyQualifiedSolutionFileName", "The solution file location is required.");
            }

            if (String.IsNullOrEmpty(projectName) || String.IsNullOrEmpty(projectName.Trim()))
            {
                throw new ArgumentNullException("projectName", "The project name is required.");
            }

            if (!File.Exists(fullyQualifiedSolutionFileName))
            {
                throw new ArgumentException(String.Format("The file {0} specified does not exist.", fullyQualifiedSolutionFileName));
            }

            if (testProjectType == null) testProjectType = TestProjectType.Unit;
            #endregion

            System.Type vsType = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0");
            Object vs = System.Activator.CreateInstance(vsType, true);
            EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)vs;

            Solution2 vhaSolution = (Solution2)dte8Obj.Solution;
            vhaSolution.Open(fullyQualifiedSolutionFileName);

            //TODO: Externalize company name
            string cmpnyName = "Vha";
            string testProjectName = String.Format("{0}.{1}.{2}{3}",cmpnyName,projectName,testProjectType.ToString(),"Test");
            string testTemplateLocation = vhaSolution.GetProjectTemplate("TestProject.zip", "CSharp");
            FileInfo rootSolutionFolder = new FileInfo(fullyQualifiedSolutionFileName);

            //TODO: Externalize test directory name
            string testDirName = String.Format("{0}\\{1}\\{2}\\{3}",rootSolutionFolder.DirectoryName,"test",testProjectType.ToString(),testProjectName);

            if (!Directory.Exists(testDirName))
            {
                //may throw an exception if the dir can't be created...
                Directory.CreateDirectory(testDirName);
            }


            Project vhaTestProj = vhaSolution.AddFromTemplate(testTemplateLocation,testDirName,testProjectName + ".proj",false);
            vhaTestProj.Save(String.Format("{0}\\{1}.proj",testDirName , testProjectName));



        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-07-24 05:26:21

我想到了。 我需要使用 prog ID VisualStudio.DTE.9.0,以便它指向 VS 2008 的正确注册表位置。

I figured it out. I need to use the prog ID VisualStudio.DTE.9.0 so that it would point to the correct registry location for VS 2008.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文