Вопрос

I am currently writing an Addin for VS2012 that is executed on an reference dll in an project.

For example: The User clicks right on an reference dll in the solution explorer, the context menu pops up and he/she/it clicks on my Addin. My exec method is called and in there I want to get the Full path of the dll he/she/it right clicked.

My Exec method looks like this:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
  handled = false;
  if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
  {
    if (commandName == "ProxyClassCreatorAddin2.Connect.ProxyClassCreatorAddin2")
    {
      string filePath = string.Empty;
      UIHierarchy uih = _applicationObject.ToolWindows.SolutionExplorer; //Also I don't know if this is correct
      Array selectedItems = (Array)uih.SelectedItems;
      if (selectedItems != null)
      {
        foreach (UIHierarchyItem item in selectedItems)
        {
          ----------------------------------------------
          Project projectItem = item.Object as Project;
          -----------------------------------------------
          filePath = projectItem.Properties.Item("FullPath").Value.ToString();
        }
      }

      handled = true;
      MessageBox.Show("SelectedItem.path:" + filepath);          
      return;
    }
  }
}

The problem is where i have selected the item.Object as Project because I dont know what is comming back there, so "projectItem" is null. I'm now googling for days and didn't found anything...

Please can anybody tell me how I can get the path of the selected dll?

Это было полезно?

Решение

Found an soulution:

public void Exec(
  string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
  handled = false;
  if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
  {
    if (commandName == "ProxyClassCreatorAddin2.Connect.ProxyClassCreatorAddin2")
    {
      string filePath = string.Empty;
      var selectedItems = (Array)_applicationObject.ToolWindows.SolutionExplorer.SelectedItems;

      if (selectedItems != null)
      {
        foreach (UIHierarchyItem item in selectedItems)
        {
          ------------------------------------------------
          dynamic obj = item.Object;
          filePath = obj.Path;
          -------------------------------------------------
        }
      }
      handled = true;
      MessageBox.Show(filePath);
      CreateProxyClasses.CreateProxyClasses form = new CreateProxyClasses.CreateProxyClasses(filePath);
      form.Show();
      return;
    }
  }
}

I already knew that "item.Object" has an "path" property so I could select it by using dynamic More about dynamic: http://msdn.microsoft.com/en-us/library/dd264741.aspx

I hope this answer can help you too!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top