4

如何从Visual Studio 2010扩展中获取默认项目创建目录?默认情况下,该目录是:在Visual Studio 2010扩展中获取默认项目目录

C:\Users\<username>\Documents\Visual Studio 2010\Projects 

我猜这是通过工具 - >选项窗口中完成,但我不知道如何在这个通过SDK获取。

乍一看下面看起来应该让我接近:

DTE2 dte = CType(GetService(typeof(SDTE)), DTE2); 
Object props = dte.Properties("Projects and Solutions", "General"); 

,但我得到了以下错误:

System.Runtime.InteropServices.COMException was unhandled by user code 
ErrorCode=-2147352565 
Message=Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) 
+1

如果您没有找到通过DTE接口获取它的方法,您可能可以从注册表位置获得它,该位置支持以下设置:HKEY_CURRENT_USER \ Software \ Microsoft \ VisualStudio \ 10.0',DefaultNewProjectLocation'。 – 2012-07-09 12:01:26

+0

@RomanR。谢谢,希望有人能告诉我我应该使用哪个索引,因为“项目和解决方案”似乎不起作用,但如果不能,这是一个很好的选择。 – 2012-07-09 14:50:35

回答

7

发现:
http://msdn.microsoft.com/en-us/library/ms165642.aspx

After Visual Studio has been run once, the property item names for the current instance of Visual Studio are stored in the following Windows registry key: HKCU\SOFTWARE\Microsoft\VisualStudio\10.0_Config\AutomationProperties. This location always has the definitive list of names. The category names are the names of the subkeys of the AutomationProperties key (Database Tools, FontsAndColors, and so on). The page names are the names of the subkeys of the category keys. For example, the FontsAndColors category contains the Dialogs and Tool Windows, Printer, and TextEditor pages. You can view the registry by using the registry editor.

这里是获取值的代码,寻找:

string defaultProjectPath = (string)(DTE.Properties("Environment", "ProjectsAndSolution").Item["ProjectsLocation"].Value); 

注意,在注册表中的值不与你在Visual Studio中那么一点点的侦探工作需要去,你要寻找的确切值看到的内容相符。我认为它是以这种方式设置的,因为它具有向后兼容性。

+0

即使在添加了envdte引用和EnvDTE命名空间之后,我仍无法运行此代码段。难道我做错了什么?另外,如果DTE.Properties应该是DTEVarName,那么您是如何构造它的?我试过DTE myDTE = new DTE();但是这并没有解决抱怨的代码行。请记住,我对DTE不太了解......我只是寻找一种以编程方式检索VS2013的默认项目目录的方法 – 2015-02-13 02:02:34

+1

由于这是近两年前,我恐怕我不记得了细节和代码被隐藏在某处。这也是VS 2010的代码,所以VS 2013可能会有所不同。提出一个新问题并提及这个问题可能是值得的,尽管我原来没有太多的运气来获得答案......也许你会有更好的运气。 – 2015-02-13 16:01:36

+0

谢谢,马特! :) – 2015-02-13 18:17:05

相关问题