4

How do I get the default project creation directory from within a Visual Studio 2010 extension? By default this directory is:

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

I'm guessing this is done via the Tools->Options window, but I don't know how to get at this via the SDK.

At first glance the following looks like it should get me close:

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

But I get the following error:

System.Runtime.InteropServices.COMException was unhandled by user code
ErrorCode=-2147352565
Message=Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Matt Ruwe
  • 3,386
  • 6
  • 39
  • 77
  • 1
    If you don't find a way to obtain it via DTE interfaces, you perhaps can obtain it from registry location which backs these settings: `HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0`, `DefaultNewProjectLocation`. – Roman R. Jul 09 '12 at 12:01
  • @RomanR. Thanks, hopefully someone will be able to tell me which index I should be using since "Projects and Solutions" doesn't appear to work, but if not, this is a good alternative. – Matt Ruwe Jul 09 '12 at 14:50

1 Answers1

8

Found it:
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.

And here's the code to get the values I'm looking for:

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

Note that the values in the registry don't match up with what you see in Visual Studio so a little detective work is required to get to the exact value that you're looking for. I assume that it's setup this way due to backward compatibility.

Matt Ruwe
  • 3,386
  • 6
  • 39
  • 77
  • I couldn't get this snippet to run, even after adding the envdte reference and the EnvDTE namespace. Am I doing something wrong? Also, if DTE.Properties is supposed to be DTEVarName instead, how did you construct it, exactly? I tried DTE myDTE = new DTE(); but that didn't fix that line of code from complaining. Please keep in mind, I don't know much about DTE... I'm just looking for a way to programmatically retrieve VS2013's default project directory – kayleeFrye_onDeck Feb 13 '15 at 02:02
  • 1
    As this was almost two years ago, I'm afraid I don't remember the details and the code is buried away somewhere. It's also code for VS 2010 so VS 2013 might be different. It would probably be worthwhile to ask a new question and refer to this one, although I didn't have much luck getting an answer originally... Maybe you'll have better luck. – Matt Ruwe Feb 13 '15 at 16:01