Open DTE solution from another program (not add-in)

时光毁灭记忆、已成空白 提交于 2019-12-07 15:55:40

问题


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:


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);
        }
    


来源:https://stackoverflow.com/questions/16994899/open-dte-solution-from-another-program-not-add-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!