locked
How to access open project from a VS window? RRS feed

  • Question

  • I have VS extensibility menu project that invokes a user control. The user control gets loaded inside VS as a window. I need to know what solution/project is open in this instance of VS. How can I get that?

    Environment: VS 2010, .Net 4.0

    Monday, October 8, 2012 4:49 AM

Answers

  • Hi Shilpa,

    follow the steps mentioned below:

    1. Create a static class with static property.

    public static class DTEObject
        {
            public static DTE2 DTEObj { get; set; }
        }

    2. Assign this property a value in OnConnection event in Connect class.

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {

    DTEObject.DTEObj = (DTE2)application;

    }

    3. Access the property in user control whereever you need. Exp.

            private void button1_Click(object sender, EventArgs e)
            {
                var obj = DTEObject.DTEObj;
            }

    And you are done.

    Hope you like this hacking.

    Mark it as Answer if it solves your problem.

    Best Regards,

    Shikhar Jain

    Friday, October 12, 2012 10:41 AM

All replies

  • Hi Shilpa,

    _applicationObject instance of DTE2 interface allows to get instance of solution and projects.

    Below is the snippet which check whether a solution is opened in IDE. Also, check if solution has any project. if it has project then get object of required project.

    if(_applicationObject.Solution!=null)
                {
                    var solution=_applicationObject.Solution;
                    var solutionName = solution.FullName;
                    if (solution.Projects.Count > 0)
                    {
                        var project = (from Project proj in solution.Projects where proj.Name.Equals("ProjName") select proj).First();
                    }
                }

    Hope this helps.

    Best Regards,

    Shikhar Jain

    Monday, October 8, 2012 9:17 AM
  • Hi Shikhar, thanks for the input.

    The user control is not present in the VS extensibility project, hence I do not have access to the _applicaitonObject. How do I get it in the user control?

    Shilpa 

    Monday, October 8, 2012 9:32 AM
  • If you are using a vsPackage, you can get the dte object like this : 

    EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDTE)) as EnvDTE.DTE;
    
    _applicationObject is the dte object in ShikharJain response
    Monday, October 8, 2012 9:37 AM
  • Thanks Mathias, I will check this out.
    Monday, October 8, 2012 10:17 AM
  • Hello Mathias, Shikhar. I tried the above code but got dte as null.

    I need to get the value of dte in a user control (residing in a library) that gets loaded as a child window in VS instance.

    Wednesday, October 10, 2012 7:12 AM
  • Can you pass DTE object from child window to the user control ?

    Add a reference to DTE to your user control, add a constructor with a DTE parameter. Or set DTE with a property of your user control. And then you'll be able to use it.

    Wednesday, October 10, 2012 7:24 AM
  • Mathias, I am using the following code to create the window in VS. The asmpath contains the path to the user control dll. The toolwin.DTE gives me the _application object. Now, the problem I am facing is, my user control is not able to identify the container it is loaded in. So, usercontrol.Parent and userControl.Container are both returning null. If I am able to get the container, then I can access the DTE as well. How do I get that?

    toolWin = ((Windows2)_applicationObject.Windows).CreateToolWindow2(_addInInstance,
      asmPath, ctlProgID, "Menu", guidStr,
      ref objTemp);

    Thursday, October 11, 2012 12:58 PM
  • Hi Shilpa,

    follow the steps mentioned below:

    1. Create a static class with static property.

    public static class DTEObject
        {
            public static DTE2 DTEObj { get; set; }
        }

    2. Assign this property a value in OnConnection event in Connect class.

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {

    DTEObject.DTEObj = (DTE2)application;

    }

    3. Access the property in user control whereever you need. Exp.

            private void button1_Click(object sender, EventArgs e)
            {
                var obj = DTEObject.DTEObj;
            }

    And you are done.

    Hope you like this hacking.

    Mark it as Answer if it solves your problem.

    Best Regards,

    Shikhar Jain

    Friday, October 12, 2012 10:41 AM
  • Thanks Shikhar and Mathias.

    I moved the user control from a separate library to the library that contains the menu. The above solution worked for me.

    Wednesday, October 17, 2012 12:56 PM