Envdte尝试到达project.StartOptions无法访问ConfigurationManager [英] Envdte Trying to reach project.StartOptions Cant access ConfigurationManager

查看:52
本文介绍了Envdte尝试到达project.StartOptions无法访问ConfigurationManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用envdte/envdte80(两者都没有经验)编写一个c#控制台应用程序,以在新的Visual Studio实例中打开一个项目.在这个新实例中,我试图通过控制台应用程序更改Project.StartOptions.

当我尝试加载ConfigurationManager时,收到此异常:
"System.InvalidOperationException",
由于对象的当前状态,操作无效."

我的代码:
    

I'm writing a c# console application with envdte / envdte80 (no experience with both) to open a project inside a new Visual Studio Instance. Inside this new instance, I'm trying to change the Project.StartOptions through my console application.

When I try to load the ConfigurationManager, I'm receiving this exception:
"System.InvalidOperationException",
"Operation is not valid due to the current state of the object."

My Code:
    

using EnvDTE;
using EnvDTE80;
using System;

namespace AutomationProject
{
    class Program
    {
        static void Main(string[] args)
        {
            ...
            EnvDTE80.DTE2 dte;
            object obj = null;
            System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.14.0", true);
            System.Threading.Thread.Sleep(1000);
            // Attempt to create an instance of envDTE. 
            obj = System.Activator.CreateInstance(t, true);

            // Cast to DTE2.
            dte = (EnvDTE80.DTE2)obj;
            dte.MainWindow.Visible = true;
            //dte.MainWindow.Activate();
            //dte.UserControl = true;
            dte.ExecuteCommand("File.OpenProject", projectLocation + projectName);
            
            //dte.ExecuteCommand("Project.StartOptions", "-break -lib:ModuleName -exec:TestName");
            // Get a reference to the solution2 object.
            System.Threading.Thread.Sleep(1000);
            Solution2 soln = (Solution2)dte.Solution;
            Project proj = soln.Projects.Item(1);
            try
            {
               dte.Solution.SolutionBuild.SolutionConfigurations.Item(1).Activate();
                ConfigurationManager configmgr;
                Configuration config;
                if (dte.Solution.Projects.Count > 0)
                {
                    configmgr = dte.Solution.Projects.Item(1).ConfigurationManager;
                    config = proj.ConfigurationManager.ActiveConfiguration;
                    config.Properties.Item("StartArguments").Value = "command line arguments";
                ...
                }
            ...
            }
        ...
        }
    }
}





推荐答案

是否有任何理由需要打开VS进行这些更改?尤其是随着VS的更新版本(其中所有内容都是异步的)的变化,仅休眠获取VS时间来加载项目是不够的.如果轻量级的话尤其如此 解决方案加载功能已打开.

Is there any reason why you need to open VS to make these changes? Especially with the changes in newer versions of VS where everything is async, simply sleeping to get VS time to load the projects isn't sufficient. This is especially true if lightweight solution loading is turned on.

//Add Microsoft.Build.Framework NuGet package first
using Microsoft.Build.Construction;

static void Main ( string[] args )
{
    var filePath = @"project filepath";

    //Open the project
    var project = ProjectRootElement.Open(filePath);

    //Find all property groups that are related to a configuration            
    var configurations = FindConfigurations(project.PropertyGroups);
    foreach (var config in configurations)
    {
        //Get the existing startup options, if nay
        var startupOptions = FindProperty(config, "StartArguments");

        //Add or update it
        if (startupOptions != null)
            startupOptions.Value = "arguments";
        else
            config.AddProperty("StartArguments", "arguments");                                
    };

    //Save changes
    project.Save();
}

//Could be extension methods...
static IEnumerable<ProjectPropertyGroupElement> FindConfigurations ( IEnumerable<ProjectPropertyGroupElement> groups )
{
    foreach (var group in groups)
    {
        if (!String.IsNullOrEmpty(group.Condition) && group.Condition.IndexOf("


(Configuration),StringComparison.OrdinalIgnoreCase)> = 0) 收益回报组; }; } 静态ProjectPropertyElement FindProperty(ProjectPropertyGroupElement源,字符​​串propertyName) { 返回source.Properties.FirstOrDefault(p => String.Compare(p.Name,propertyName,true)== 0); }
(Configuration)", StringComparison.OrdinalIgnoreCase) >= 0) yield return group; }; } static ProjectPropertyElement FindProperty ( ProjectPropertyGroupElement source, string propertyName ) { return source.Properties.FirstOrDefault(p => String.Compare(p.Name, propertyName, true) == 0); }

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于Envdte尝试到达project.StartOptions无法访问ConfigurationManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆