如何从编辑器分类项目(c#)获取当前解决方案目录?

4

从插件中,我们可以通过以下方式获取解决方案路径。

_applicationObject = (DTE2)application;  // retrieved from OnConnection method
string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);

您如何通过编辑器分类器项目(Visual C#->可扩展性->编辑器分类器)实现此操作?
无法从应用程序或使用getService方法获取DTE对象。
可以有人帮我吗?提前致谢。
 internal class TestQuickInfoSource : IQuickInfoSource
    {
        private TestQuickInfoSourceProvider m_provider;
        private ITextBuffer m_subjectBuffer;
        private Dictionary<string, string> m_dictionary;
        private bool m_isDisposed;
        private DTE2 _applicationObject;

        [Import]
        internal SVsServiceProvider ServiceProvider = null;

        //constructor that initializes the dictionary
        public TestQuickInfoSource(TestQuickInfoSourceProvider provider, ITextBuffer subjectBuffer)
        {
            m_provider = provider;
            m_subjectBuffer = subjectBuffer;

              **// HERE the dictionary with the tool-tip info is created. I would need the solution path here ideally so that I can dynamically create the dictionary from a file there. Following commented statements.**

            // Get an instance of the currently running Visual Studio IDE
            //DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
            //string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

            //DTE dte = (DTE)ServiceProvider.GetService(typeof(EnvDTE.DTE));

            //string solutionPath = Path.GetDirectoryName(dte.Solution.FullName);

            //_applicationObject = (DTE2)provider;

            //string solutionPath = Path.GetDirectoryName(_applicationObject.Solution.FullName);            


            //string gg = System.Reflection.Assembly.GetExecutingAssembly().ToString();

            //string solutionDirectory = ((EnvDTE.DTE)System.Runtime
            //                                  .InteropServices
            //                                  .Marshal
            //                                  .GetActiveObject("VisualStudio.DTE.10.0"))
            //                       .Solution
            //                       .FullName;



            //EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
            //string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

            //_applicationObject = (DTE2)dte;

            //string solutionDir1 = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);


            string startupPath = System.IO.Directory.GetCurrentDirectory();

            //EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
            //System.IO.Path.GetDirectoryName(dte.Solution.FullName);

            //Reading Resource File
            //string path = "";



            //EnvDTE80.DTE2 dte2;
            //dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");

            //_applicationObject = (DTE2)dte2;

            //if (_applicationObject.ActiveDocument != null)
            //{
           //    string y = _applicationObject.ActiveDocument.ToString();
            //    string solutionPat = Path.GetDirectoryName(dte2.Solution.FullName);                
            //}

            //Connect objConnect = new Connect();
            //Array objArray = null;
            //objConnect.OnConnection(dte2, ext_ConnectMode.ext_cm_AfterStartup, null, ref objArray);


            //these are the method names and their descriptions
            m_dictionary = new Dictionary<string, string>();
            m_dictionary.Add("add", "int add(int firstInt, int secondInt)\nAdds one integer to another.");
            m_dictionary.Add("subtract", "int subtract(int firstInt, int secondInt)\nSubtracts one integer from another.");
            m_dictionary.Add("multiply", "int multiply(int firstInt, int secondInt)\nMultiplies one integer by another.");
            m_dictionary.Add("divide", "int divide(int firstInt, int secondInt)\nDivides one integer by another.");
            m_dictionary.Add("#include", "Custom Tool-Tip !");
        }

请查看加粗的注释以了解问题。

你能展示一下你已经尝试过的代码吗? - frank koch
也许你应该在你的问题中加上标签[vs-extensibility]和/或[visual-studio-extensions]。 - frank koch
请查看粗体字中的注释以了解问题。 - Srinivasa Rao Reddi
2个回答

3
您可以使用GetGlobalService,如下所示:
var dte2 = (DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDTE));

通常情况下,@Srikanth Venugopalan提供的方法可以解决问题。如果返回“NULL”,说明出现了问题,如果没有更多信息,那么很难给予帮助。


很酷但有漏洞:http://stackoverflow.com/questions/33209589/project-names-in-visual-studio-solution-sometimes-are-empty - alerya

0

你可以尝试导入SVsServiceProvider并使用GetService方法获取实例。

这个演示文稿向你展示了如何获取对DTE对象的引用。

一旦你有了DTE引用,其余的步骤应该与添加中所做的步骤类似。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接