الحصول على برمجيا الدليل الحالي البصرية حل ستوديو IDE من الوظائف الإضافية

StackOverflow https://stackoverflow.com/questions/2054182

سؤال

ولدي بعض الأدوات التي إجراء تحديثات على الحلول .NET، ولكنها تحتاج إلى معرفة الدليل حيث يوجد الحل.

وأضفت هذه الأدوات كأدوات الخارجية، حيث تظهر في القائمة أدوات IDE، وتوريد $(SolutionDir) كحجة. هذا يعمل بشكل جيد.

ولكن، أريد هذه الأدوات لتكون أسهل في الوصول في IDE للمستخدم من خلال القائمة المستوى الأعلى حسب الطلب (الذي أنا خلقت مشروع Visual Studio حزمة التكامل) ومن خلال السياق القائمة على العقد الحل (الذي أنا خلق البصرية ستوديو الإضافية في المشروع). أنا أبحث عن وسيلة للحصول على دليل الحل الحالي من خلال هذه السياقات.

وحاولت الحصول على المعلومات حل من وجوه VisualStudio.DTE:

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

ولكن، وهذا يعود الدليل حل للالوظائف الإضافية، وليس الحل الحالي.

وحاولت مرددا $(SolutionDir) وقراءته مرة أخرى:

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "echo $(SolutionDir)");

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();

ولكن، هذا عاد الدليل لIDE، وليس الحل الحالي.

وأنا لا يرى أي معلومات ذات صلة في CommandBar عقدة الحل.

وبدلا من ذلك، إذا كان هناك طريقة للوصول برمجيا استوديو الأدوات البصرية الخارجية محددة وإطلاقها (باستخدام الحجج الكلية محددة مسبقا)، فإن هذا العمل.

ما هو الحل؟

هل كانت مفيدة؟

المحلول

<اقتباس فقرة>   

وEnvDTE.DTE DTE =   (EnvDTE.DTE) System.Runtime.InteropServices.Marshal.GetActiveObject ( "VisualStudio.DTE")؛   سلسلة solutionDir =   System.IO.Path.GetDirectoryName (dte.Solution.FullName)؛

     

ولكن، وهذا يعود الحل   الدليل لالوظائف الإضافية، وليس   الحل الحالي.

ونهجكم للحصول على دليل جيد. ما هو الخطأ هي الطريقة تحصل على وجوه VisualStudio.DTE. حيث دعا هذا الرمز؟ أفترض أنه في الوظيفة الإضافية. هل تنفيذ (تصحيح) الخاص بك الوظيفة الإضافية في Visual Studio الذي يفتح مثال آخر من Visual Studio حيث يمكنك فتح الحل؟ ولذلك عليك أن حالتين من Visual Studio.

ووGetActiveObject("VisualStudio.DTE") يحصل على سبيل المثال برنامج Visual Studio عشوائي. في الحالة الخاصة بك، فإنه على ما يبدو البصرية ستوديو مع وظيفة إضافية في المشروع منذ تحصل الطريق إلى الوظيفة الإضافية الخاصة بك. أن لشرح ما يمكن أن يكون سبب مشكلتك.

والطريقة الصحيحة للحصول على DTE بسيطة جدا. في الواقع، الوظيفة الإضافية الخاصة بك بالفعل إشارة إلى DTE الذي يتم تشغيله (أي، الذي فتح الحل). يتم تخزينها في _applicationObject متغير عمومي في الوظيفة الإضافية الخاصة بك الدرجة الاتصال. وهو يقع عند الوظيفة الإضافية الخاصة بك يبدأ في معالج الأحداث OnConnection. لذلك كل ما عليك هو الاتصال على:

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

نصائح أخرى

ومع دفع بطرس في الاتجاه الصحيح، أقوم بإعداد والدين قائمة السياق لإطلاق أداة خارجية مع الدليل الحل، وإخراج النتائج إلى جزء الانتاج. بعض الأمثلة دعاية مغالى فيها من الوظيفة:

    ///--------------------------------------------------------------------------------
    /// <summary>This method implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
    ///
    /// <param term='application'>Root object of the host application.</param>
    /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
    /// <param term='addInInst'>Object representing this Add-in.</param>
    /// <seealso class='IDTExtensibility2' />
    ///--------------------------------------------------------------------------------
    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

        // Get the solution command bar
        CommandBar solutionCommandBar = ((CommandBars)_applicationObject.CommandBars)["Solution"];

        // Set up the main InCode
        CommandBarPopup solutionPopup = (CommandBarPopup)solutionCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, true);
        solutionPopup.Caption = "InCode";

        // Add solution updater submenu
        CommandBarControl solutionUpdaterControl = solutionPopup.Controls.Add(MsoControlType.msoControlButton, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, true);
        solutionUpdaterControl.Caption = "Update Solution";
        updateSolutionMenuItemHandler = (CommandBarEvents)_applicationObject.Events.get_CommandBarEvents(solutionUpdaterControl);
        updateSolutionMenuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(updateSolution_Click);
    }

    // The event handlers for the solution submenu items
    CommandBarEvents updateSolutionMenuItemHandler;

    ///--------------------------------------------------------------------------------
    /// <summary>This property gets the solution updater output pane.</summary>
    ///--------------------------------------------------------------------------------
    protected OutputWindowPane _solutionUpdaterPane = null;
    protected OutputWindowPane SolutionUpdaterPane
    {
        get
        {
            if (_solutionUpdaterPane == null)
            {
                OutputWindow outputWindow = _applicationObject.ToolWindows.OutputWindow;
                foreach (OutputWindowPane loopPane in outputWindow.OutputWindowPanes)
                {
                    if (loopPane.Name == "Solution Updater")
                    {
                        _solutionUpdaterPane = loopPane;
                        return _solutionUpdaterPane;
                    }
                }
                _solutionUpdaterPane = outputWindow.OutputWindowPanes.Add("Solution Updater");
            }
            return _solutionUpdaterPane;
        }
    }

    ///--------------------------------------------------------------------------------
    /// <summary>This method handles clicking on the Update Solution submenu.</summary>
    ///
    /// <param term='inputCommandBarControl'>The control that is source of the click.</param>
    /// <param term='handled'>Handled flag.</param>
    /// <param term='cancelDefault'>Cancel default flag.</param>
    ///--------------------------------------------------------------------------------
    protected void updateSolution_Click(object inputCommandBarControl, ref bool handled, ref bool cancelDefault)
    {
        try
        {
            // set up and execute solution updater thread
            UpdateSolutionDelegate updateSolutionDelegate = UpdateSolution;
            updateSolutionDelegate.BeginInvoke(UpdateSolutionCompleted, updateSolutionDelegate);
        }
        catch (System.Exception ex)
        {
            // put exception message in output pane
            SolutionUpdaterPane.OutputString(ex.Message);
        }
    }

    protected delegate void UpdateSolutionDelegate();

    ///--------------------------------------------------------------------------------
    /// <summary>This method launches the solution updater to update the solution.</summary>
    ///--------------------------------------------------------------------------------
    protected void UpdateSolution()
    {
        try
        {
            // set up solution updater process
            string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"SolutionUpdater.exe", solutionDir);
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;

            // execute the solution updater
            proc.Start();

            // put solution updater output to output pane
            SolutionUpdaterPane.OutputString(proc.StandardOutput.ReadToEnd());
            SolutionUpdaterPane.OutputString("Solution update complete.");
        }
        catch (System.Exception ex)
        {
            // put exception message in output pane
            SolutionUpdaterPane.OutputString(ex.Message);
        }
    }

    ///--------------------------------------------------------------------------------
    /// <summary>This method completing the update solution thread.</summary>
    ///
    /// <param name="ar">IAsyncResult.</param>
    ///--------------------------------------------------------------------------------
    protected void UpdateSolutionCompleted(IAsyncResult ar)
    {
        try
        {
            if (ar == null) throw new ArgumentNullException("ar");

            UpdateSolutionDelegate updateSolutionDelegate = ar.AsyncState as UpdateSolutionDelegate;
            Trace.Assert(updateSolutionDelegate != null, "Invalid object type");

            updateSolutionDelegate.EndInvoke(ar);
        }
        catch (System.Exception ex)
        {
            // put exception message in output pane
            SolutionUpdaterPane.OutputString(ex.Message);
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top