在Visual C#2015中获取DTE对象的引用

在Visual C#2015中获取DTE对象的引用,c#,C#,我想在VisualStudio2015中使用带有C#的DTE对象来获取对当前解决方案的引用 using System; using EnvDTE; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; namespace TemplatesExample { class Program { static void Main(string[] args)

我想在VisualStudio2015中使用带有C#的DTE对象来获取对当前解决方案的引用

using System;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace TemplatesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            IVsSolution solution = Package.GetGlobalService(typeof(DTE)) as IVsSolution;

            Console.WriteLine(solution.ToString());

            Console.ReadKey();

        }

    }
}
但是当我使用它时,我的解决方案对象总是空的


那么,如何使用.net framework 4.6上的C#访问VS2015中当前的解决方案对象呢?

试试这个示例。在VS2015上启动并运行。 (此方法仅对相同的解决方案有效)

我已经遵循了解决方案,但问题仍然并没有得到解决。
using EnvDTE;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public class DTEHandle
        {
            //EnvDTE.Project proj;
            //EnvDTE.Configuration config;
            //EnvDTE.Properties configProps;
            //EnvDTE.Property prop;
            EnvDTE.DTE DTE = Marshal.GetActiveObject("VisualStudio.DTE.14.0") as EnvDTE.DTE;
            public EnvDTE.Project GetProject(String Name)
            {
                foreach (EnvDTE.Project item in DTE.Solution.Projects)
                {
                    if (item.Name == Name)
                    {
                        return item;
                    }
                }
                return null;
            }
        }

        public Form1()
        {
            InitializeComponent();
            EnvDTE.DTE DTE = Marshal.GetActiveObject("VisualStudio.DTE.14.0") as EnvDTE.DTE;

            DTEHandle h = new DTEHandle();
            EnvDTE.Project proj = h.GetProject("Test");

            foreach (EnvDTE.ProjectItem item in proj.ProjectItems)
            {
                if (item.Name == "Program.cs")
                {
                    TextSelection s = item.Document.Selection as TextSelection;
                    s.SelectAll();
                    MessageBox.Show(s.Text);
                }
            }          
        }
    }
}