visual-studio 如何从 VSPackage 获取当前解决方案目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2336818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get the current solution directory from a VSPackage?
提问by Dave Clemmer
Following is how you would get the current solution directory from an add-in:
以下是从加载项获取当前解决方案目录的方法:
_applicationObject = (DTE2)application; // retrieved from OnConnection method
string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);
How would you do this via a VSPackage?
你将如何通过 VSPackage 做到这一点?
I'm migrating a visual studio add-in to a VSPackage as I'm intending to add some user controls that require a deeper integration with the IDE.
我正在将 Visual Studio 加载项迁移到 VSPackage,因为我打算添加一些需要与 IDE 进行更深入集成的用户控件。
I found some good references on the relative merits of add-ins vs integration packages such as: http://nayyeri.net/visual-studio-addin-vs-integration-package-part-1
我发现了一些关于加载项与集成包的相对优点的很好的参考资料,例如:http: //nayyeri.net/visual-studio-addin-vs-integration-package-part-1
And some good tutorials on msdn on VSPackages such as: http://msdn.microsoft.com/en-us/library/cc138589.aspx
还有一些关于 VSPackages 的 msdn 的好教程,例如:http: //msdn.microsoft.com/en-us/library/cc138589.aspx
I haven't found a good reference yet (on msdn or otherwise) on how the higher level interfaces in add-ins (such as DTE) map to lower level interfaces in VSPackages.
我还没有找到一个很好的参考资料(在 msdn 或其他方式上)关于加载项(例如 DTE)中的更高级别的接口如何映射到 VSPackages 中的更低级别的接口。
Any good references out there to help with general mapping from add-in interfaces to VSPackage interfaces?
有什么好的参考资料可以帮助从加载项接口到 VSPackage 接口的一般映射?
回答by Dave Clemmer
I found the answer to the specific question. The VisualStudio.DTE object can be retrieved via the GetService()method as follows:
我找到了特定问题的答案。可以通过以下GetService()方法检索 VisualStudio.DTE 对象:
// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);
回答by Medeni Baykal
You can get a DTE object from one of these functions:
您可以从以下函数之一获取 DTE 对象:
public static DTE GetCurrentDTE(IServiceProvider provider)
{
/*ENVDTE. */DTE vs = (DTE)provider.GetService(typeof(DTE));
if (vs == null) throw new InvalidOperationException("DTE not found.");
return vs;
}
public static DTE GetCurrentDTE()
{
return GetCurrentDTE(/* Microsoft.VisualStudio.Shell. */ServiceProvider.GlobalProvider);
}
After that, you can get active Solutionfrom DTE.Solutionand Solutionpath from DTE.Solution.Pathproperty.
在这之后,你可以得到积极Solution的DTE.Solution和 Solution从路径DTE.Solution.Path属性。
回答by Bondolin
If using the IVsSolutioninterface, you can use GetSolutionInfoto obtain the path of the solution, the solution filename, and the solution user options (SUO) filename:
如果使用该IVsSolution接口,您可以使用GetSolutionInfo来获取解决方案的路径、解决方案文件名和解决方案用户选项 (SUO) 文件名:
this.solution.GetSolutionInfo(
out string solutionDirectory,
out string solutionFile,
out string userOptsFile);

