Reputation: 107
I'm new to Visual Studio Extensibility Framework to use VSPackage Extension. I want to obtain a DTE Object inside the user-control which is called inside MyToolWindow class.
I tried all the below possibilities:
1.EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
string solutionPath=dte2.Solution.FullName;
The above trial gives only SolutionPath of the 1st instance of the visual studio which is opened. For EX: If Math1.sln and Math2.sln is opened,only Math1.sln path is assigned to solutionPath.
Once, DTE object is obtained I shall be easily be able to obtain solution Name of the current instance.
I must be able to obtain appropriate EnvDTE80.DTE object inside the click of the button which is inside User-control. This User-Control is called inside MyToolWindow.cs
Any help on this would be greatly appreciated.
Upvotes: 0
Views: 966
Reputation: 5508
I have an abstract VsPackage class I use within all my home-brewed extensions; and I obtain the application instance this way...
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
public abstract class VsPackage : Package
{
...
protected DTE2 GetApplication()
{
return this.GetService(typeof(SDTE)) as DTE2;
}
...
}
Upvotes: 0
Reputation: 4423
First of all, Marshal.GetActiveObject("VisualStudio.DTE.10.0")
is problematic because it will just give you a DTE
of a random Visual Studio instance. You can obtain correct instance of DTE
through the GetService
call.
Then just enumerate the DTE.Solution.Properties
, one of the properties should be the solution name. Probably DTE.Solution.Properties.Item("Name")
, though I cannot check this at the moment.
As for your "GetService cannot be recognized", method is defined on an IServiceProvider
which ToolWindowPane
implements. If you want to use that in your control, you should pass the tool window instance to your control. Example:
public class MyToolWindow: ToolWindowPane {
void SomeMethod() {
var myControl = new MyControl(this);
}
}
public class MyControl: UserControl {
public MyControl(IServiceProvider serviceProvider) {
// Now you can call serviceProvider.GetService
}
}
Upvotes: 3