Search code examples
c#visual-studio-extensionsvsix

How to set and receive arguments to custom visual studio project command


I have created a custom command for the visual studio project and the command is visible on the project right-click. I wanted to pass some arguments like project folder path to the command EventArgs. Is it possible to specify arguments to the command.

Here is the command Execution method.

private void Execute(object sender, EventArgs e)
{
    ThreadHelper.ThrowIfNotOnUIThread();
    string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", GetType().FullName);
    string title = "Evaluate Project Command";

    // Show a message box to prove we were here
    VsShellUtilities.ShowMessageBox(
                                    package,
                                    message,
                                    title,
                                    OLEMSGICON.OLEMSGICON_INFO,
                                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}

I want to get the Project file path from the command EventArgs or is there any way to do it?


Solution

  • The EventArgs in the method signature for the command execution does not typically contain this data directly. However, you can retrieve the project details by accessing the DTE object, which represents the development environment itself.

    Take a look at this example:

    https://github.com/NDiiong/DevAssist/blob/3a90dbbbd42c57dd74749f01e26ba30aeac68732/src/Commands/OpenBinFolderCommand.cs#L18