本文介绍了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";
                ...
                }
            ...
            }
        ...
        }
    }
}





推荐答案

//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("


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

Michael Taylor
http://www.michaeltaylorp3.net


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

10-28 18:46