簡體   English   中英

如何從編輯器分類器項目(c#)獲取當前的解決方案目錄?

[英]How do you get the current solution directory from a Editor Classifier Project (c#)?

從加載項中,我們可以使用以下方式獲取解決方案路徑。

_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”,就會出現問題,而且不知道更多,這很難提供幫助。

您可以嘗試使用GetService方法導入SVsServiceProvider並獲取實例

本演練為您提供了如何獲取DTE對象引用的說明。

一旦你有DTE參考,其余的應該類似於你在add-n中做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM