Open DTE solution from another program (not add-in

2019-05-13 17:44发布

Is it possible to modify a solution, and use envdte tools, from a command-line project ?

I have an add-in that modifies a solution. But... the changes are required for over a hundred projects... So I'd like to make a c# program that has the same logic, only it iterates through all solution files.

The add-in starts with

EnvDTE.Solution solution = (EnvDTE.Solution)application.Solution;

where DTE2 application is passed from the add-in...

How can I get the same solution, which then I query for projects... From a separate program, that will only know the solutionPath ?

Is it possible to open the solution, process it, and close it - to move on to the next solution ?

Microsoft gives this example http://msdn.microsoft.com/en-us/library/envdte._solution.open(v=vs.100).aspx

But I don't know what dte is in the context...

Thank you...

VS 2010

edit: I did what the answer below suggests. Slightly modified, using the link: http://msdn.microsoft.com/en-us/library/ms228772(v=vs.100).aspx

Thank you

1条回答
男人必须洒脱
2楼-- · 2019-05-13 18:15

Yes you can. You just need to activate an instance using the COM CLSID for Visual Studio. An example is below. It actually creates a solution and adds two projects to it but the same initialization applies when opening an existing solution.

A couple of caveats:

  1. Mind the COM threading model. The code created from the console app template is sufficient:

    [STAThread]
    static void Main()
    
  2. If you have a powerful VS extension like ReSharper installed, you might be better off suspending it if you don't need it for the VS automation. ReSharper had VS commands that control it.

        Console.WriteLine("Opening Visual Studio");
        var dte = (DTE)Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.DTE.10.0",true),true);
    
        Console.WriteLine("Suspending Resharper");
        dte.ExecuteCommand("ReSharper_Suspend");
    
        Console.WriteLine("Working with {0}, {1} edition", dte.FullName, dte.Edition);
        dte.SuppressUI = true;
        dte.UserControl = false;
    
        foreach (var solution in mySolutionInfoList)
        {
            try
            {                    
                dte.Solution.Create(solution.directory, solution.name);                    
                dte.Solution.AddFromTemplate(csharpTemplatePath, solution.directory + "ClassLibrary1", "ClassLibrary1");
                dte.Solution.AddFromTemplate(vcTemplatePath, solution.directory + "Win32Dll", "Win32Dll");
                Directory.CreateDirectory(solution.directory); // ensure directory exists. Otherwise, user will be asked for save location, regardless of SupressUI value
                dte.Solution.Close(true); 
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
        }
    
        Console.WriteLine("Resuming Resharper");
        dte.ExecuteCommand("ReSharper_Resume");
    
        try
        {
            dte.Quit();
        }
        catch (Exception e)
        {
            Console.Error.WriteLine(e);
        }
    
查看更多
登录 后发表回答