locked
How to use DTE.ExecuteCommand and how to get list of available commands? RRS feed

  • Question

  • Dear coders!

    Google has not been my friend on this one (surprise, neither has Bing).

    I want to automate VS2008 externally (basically, start it, open a solution, rebuild it, and check for errors). Kind of my version of a build server.. don't ask, it kind of makes sense given other circumstances.

    I came across the EnvDTE.DTE and the ExecuteCommand, but cannot find any documentation on it.

    So.. is there documentation, and is there an easy-to-use way of using it? Like a macro recorder or something?

    (I found this http://skysigal.xact-solutions.com/Blog/tabid/427/entryid/1550/Visual-Studio-Macros-Where-to-find-the-full-list-of-commands-available-for-DTE-ExecuteCommand.aspx which does not work for me - immediate window is dead, and only seems to recognize commands that are valid in the current context like when debugging.)

    Best wishes

    Andreas

    Thursday, July 7, 2011 2:32 PM

Answers

  • - You can use the command-line of VS devenv.exe with the /build /out switches to build a solution and get a log file that you can parse:

     

    Devenv Command Line Switches: 

    http://msdn.microsoft.com/en-us/library/xee0c8y7%28VS.80%29.aspx

    or:

    - You can automate EnvDTE.DTE from an external program. See:

    HOWTO: Automating Visual Studio .NET from outside the IDE.
    http://www.mztools.com/resources_vsnet_addins.aspx

    and build it without commands. See:

    BUG: SolutionBuild.Build method builds only one project in Visual Studio add-ins or macros
    (same link)

    BTW, you can get a command with DTE.Commands.Item("xxx") and guess if it is available with Command.IsAvailable. The list of commands is in the Tools, Options window, Environment, Keyboard section.


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    Thursday, July 7, 2011 7:05 PM
  • What do you mean by 'not having any documentation'?  These are all documented on MSDN.  Do they have a code sample of doing the exact thing you are trying to do? Probably not, that would be impossible to do for every possible use of these things. There is quite a bit of information here about commands in general.

    >Is there an insanely powerful array of functionality that due to lack of documentation lives way below its potential?

    I am not sure what you mean by this, tons of people use DTE to do all kinds of things, so I don't know if it is 'living below its potential'.

    >Anything more than the Macro Recorder and then editing the results? Guessing about what would be available?

    If you were to explain what you are trying to do in detail someone can probably help.  Assuming your original description is roughly what you want then you probably would do something like:

    DTE.ExecuteCommand("File.OpenProject", "<path to solution to open here>");
    DTE.ExecuteCommand("Build.RebuildSolution");

    It may be more complicated if the actions aren't synchronous.  As for 'checking for errors' that doesn't involve any kind of command execution, you likely would need to interact with the error list or some other mechanism for determining if any errors occured.

    As for finding commands, you can use the command window, it has 'intellisense like' capabilities to show you what named commands exist.  Just start typing.  It is driven off canonical names which are (roughly) the name of the rootmost menu the item is located on (say the File menu) then the name of the command minus spaces, punctuation, etc..  So for instance, File.Exit, File.OpenProject, Help.About, etc..

    Ryan

    • Marked as answer by Yi Feng Li Friday, July 22, 2011 8:24 AM
    Thursday, July 7, 2011 6:52 PM

All replies

  • You would use DTE.Commands to get a list of commands, DTE.ExecuteCommand is for, well, executing them.  As for 'only seems to recognize commands that are valid in the current context', yes, that is a fundamental part of VS' command system.  Only commands that are applicable are executable.  There is no way 'around' that.

    Ryan

    Thursday, July 7, 2011 3:13 PM
  • You would use DTE.Commands to get a list of commands, DTE.ExecuteCommand is for, well, executing them.  As for 'only seems to recognize commands that are valid in the current context', yes, that is a fundamental part of VS' command system.  Only commands that are applicable are executable.  There is no way 'around' that.

    Ryan


    Thanks!

    About the immediate window: what you say makes sense. I mentioned this, because it was given as a way to get the commands in the blog/link I posted.

    What do you mean by DTE.Commands? If I use this on my instance, intellisense gives me some functions, but for example build is missing and so is file.

    Thursday, July 7, 2011 4:02 PM
  • DTE.Commands is a collection of Command objects, one for each command the IDE knows about.  As for intellisense, it won't be able to give you any help for specific commands, VS is an extensible environment, the only way the object could provide Intellisense is if we knew all commands ahead of time and added fields/methods for invoking each one.  Apart from being non-scalable (VS has over 4,000 commands) it obviously wouldn't work in the face of an application that can have third parties add commands.  You want to do something like a foreach over the DTE.Commands object (it is IEnumerable).

    Ryan

    Thursday, July 7, 2011 4:51 PM
  • So how do I use it, not knowing where to start? Not having any documentation?

    Is there an insanely powerful array of functionality that due to lack of documentation lives way below its potential?

    Anything more than the Macro Recorder and then editing the results? Guessing about what would be available?

    (Sorry if my question wasn't clearer - this is the essence of my question.)

    Thursday, July 7, 2011 5:09 PM
  • What do you mean by 'not having any documentation'?  These are all documented on MSDN.  Do they have a code sample of doing the exact thing you are trying to do? Probably not, that would be impossible to do for every possible use of these things. There is quite a bit of information here about commands in general.

    >Is there an insanely powerful array of functionality that due to lack of documentation lives way below its potential?

    I am not sure what you mean by this, tons of people use DTE to do all kinds of things, so I don't know if it is 'living below its potential'.

    >Anything more than the Macro Recorder and then editing the results? Guessing about what would be available?

    If you were to explain what you are trying to do in detail someone can probably help.  Assuming your original description is roughly what you want then you probably would do something like:

    DTE.ExecuteCommand("File.OpenProject", "<path to solution to open here>");
    DTE.ExecuteCommand("Build.RebuildSolution");

    It may be more complicated if the actions aren't synchronous.  As for 'checking for errors' that doesn't involve any kind of command execution, you likely would need to interact with the error list or some other mechanism for determining if any errors occured.

    As for finding commands, you can use the command window, it has 'intellisense like' capabilities to show you what named commands exist.  Just start typing.  It is driven off canonical names which are (roughly) the name of the rootmost menu the item is located on (say the File menu) then the name of the command minus spaces, punctuation, etc..  So for instance, File.Exit, File.OpenProject, Help.About, etc..

    Ryan

    • Marked as answer by Yi Feng Li Friday, July 22, 2011 8:24 AM
    Thursday, July 7, 2011 6:52 PM
  • - You can use the command-line of VS devenv.exe with the /build /out switches to build a solution and get a log file that you can parse:

     

    Devenv Command Line Switches: 

    http://msdn.microsoft.com/en-us/library/xee0c8y7%28VS.80%29.aspx

    or:

    - You can automate EnvDTE.DTE from an external program. See:

    HOWTO: Automating Visual Studio .NET from outside the IDE.
    http://www.mztools.com/resources_vsnet_addins.aspx

    and build it without commands. See:

    BUG: SolutionBuild.Build method builds only one project in Visual Studio add-ins or macros
    (same link)

    BTW, you can get a command with DTE.Commands.Item("xxx") and guess if it is available with Command.IsAvailable. The list of commands is in the Tools, Options window, Environment, Keyboard section.


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    Thursday, July 7, 2011 7:05 PM
  • (Sorry for the late reply, I was ill for some time.)

    This is exactly what I was looking for, Ryan, many thanks!

    Tuesday, July 26, 2011 10:58 AM
  • I was actually looking for the information that Carlos had pointed out to. Happened to find the info in this post.

    "..... The list of commands is in the Tools, Options window, Environment, Keyboard section."

    Many thanks to Carlos!!!

    Thursday, January 5, 2012 1:19 PM
  • No, there are over 4k commands in VS.

    Ryan

    Monday, December 10, 2012 3:41 PM
  • Hello gentlemen,

    I have a question related to DTE.ExecuteCommand. I think it is related to the topic discussed in this thread. As you know DTE.ExecuteCommand takes two strings as parameters.

    The first one is the name of the command (Action.CreateNewShortcut) and the second one is the arguments that the command takes.

    The problem is that some commands require a variable number of arguments and I don't know the order, etc. For example I guess Action.CreateNewShortcut needs at least two arguments: the action to be run when the shortcut is executed (Build.RebuildSolution) and the  shortcut itself ( Alt+O).

    Is there a window/log or something that we can check after running a command in the VS to figure out the number of arguments, their order, etc?


    Thanks in advance,





    Thursday, December 13, 2012 1:00 AM
  • Hi 

    I have use DTE.ExecuteCommand("Project.UnloadProject", "") line for unload project. but When I have run this code, I get Exception like "Command Project.UnloadProject not available". 

    Can you please help me to resolve this issue.

    Thanks,
    Jaya

    Thursday, February 16, 2017 4:31 AM