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

标签 c# visual-studio-extensions vs-extensibility

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

_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 !");
        }

请检查粗体评论以了解问题

最佳答案

您可以像这样使用 GetGlobalService:

var dte2 = (DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDTE));

但您通常采用的方式是由@Srikanth Venugopalan 提供的。如果你得到“NULL”,说明有问题,而且不了解更多,很难提供帮助。

关于c# - 如何从编辑器分类器项目 (c#) 获取当前解决方案目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183811/

相关文章:

c# - 使用适用于 Windows 和 iOS 的照片创建 vCard - C#

c# - .NET 中的 OraOLEDB 提供程序在 CLOB 字段上是否不可靠?

visual-studio-2010 - Visual Studio 2010 中服务提供商的最终列表?

c# - 是否有 Visual Studio 加载项的配置类型文件?

c# - 将二进制文件中的类型反序列化为不同应用程序中的不同命名空间(C# .NET)

c# - 默认 Azure 移动应用程序后端返回 500

c# - 是否有用于排序方法的 Visual Studio 2010 扩展?

visual-studio-extensions - 如何在 Visual Studio 项目模板中使用变量设置文件名

css - UI 元素的 NativeScript 边框半径不起作用

wpf - 如何防止 VS Shell 在 VSPackage ToolWindow 中获得键/命令绑定(bind)的优先级?