Question

I 've written an VS 2012 Addin and now I want to get the file which was clicked when opening my addin.

This is the OnConnection Method:

public void OnConnection(object application, ext_ConnectMode connectMode, object   addInInst, ref Array custom)
{
  _applicationObject = (DTE2)application;
  _addInInstance = (AddIn)addInInst;
  if (connectMode == ext_ConnectMode.ext_cm_UISetup)
  {
    object[] contextGUIDS = new object[] { };
    Commands2 commands = (Commands2)_applicationObject.Commands;
    Microsoft.VisualStudio.CommandBars.CommandBar standardToolBar =
      ((Microsoft.VisualStudio.CommandBars.CommandBars)
      _applicationObject.CommandBars)["Reference Item"];

    try
    {
      //Add a command to the Commands collection:
      Command command = commands.AddNamedCommand2(
        _addInInstance,
        "ProxyClassCreatorAddin",
        "ProxyClassCreatorAddin",
        "Executes the command for ProxyClassCreatorAddin",
        true,
        2677,
        ref contextGUIDS,
        (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
        (int)vsCommandStyle.vsCommandStylePictAndText,
        vsCommandControlType.vsCommandControlTypeButton);

      //Add a control for the command to the tools menu:
      if ((command != null) && (standardToolBar != null))
      {
        CommandBarControl ctrl =
              (CommandBarControl)command.AddControl(standardToolBar, 1);
        ctrl.TooltipText = "Executes the command for MyAddin";
      }
    }
    catch (System.ArgumentException)
    {
      //If we are here, then the exception is probably because a command with that name
      //  already exists. If so there is no need to recreate the command and we can 
      //  safely ignore the exception.
    }
  }
}

So the user clicks right on the selected reference and then my addin starts but the varIn (of the Exec method) is empty, how can i get the file/filename/path of the selected reference?

edit: VSIX is not possible

Was it helpful?

Solution

I found an answer in the exec method. The trick is to get the UIHierachy (In my project it's the SolutionExplorer) and the get the selectedItems. In this way, my program uses the first project found of all selected items.

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
  handled = false;
  if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
  {
    if (commandName == "ProxyClassCreatorAddin.Connect.ProxyClassCreatorAddin")
    {
      string filePath = string.Empty;
      UIHierarchy uih = _applicationObject.ToolWindows.SolutionExplorer;
      Array selectedItems = (Array)uih.SelectedItems;
      if (selectedItems != null)
      {
        foreach (UIHierarchyItem item in selectedItems)
        {
          var x = item.Object.GetType();
          Project projectItem = item.Object as Project;
          filePath = projectItem.Properties.Item("FullPath").Value.ToString();
        }
      }

      handled = true;
      CreateProxyClasses.CreateProxyClasses form = new CreateProxyClasses.CreateProxyClasses(filePath);
      form.ShowDialog();
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top