none
Visual Studio 2010 - Solution Explorer - Get Path of selected file (Compare code Add-In)

    Question

  • Hi :-)

    I was able to make running this code in Visual Studio 2010 (vsix) http://www.scootersoftware.com/vbulletin/archive/index.php/t-3313.html (Compare code in Visual Studio IDE) but there is one problem, it works only for root Solution Explorer files..

    How do I get easily a physical path of selected file in Solution Explorer on any position in hierarchy? Is there any non documented function which is used by Visual Studio Developers? If they can open the file on double click I guess they have to use already such a functionality?

    Well, if not can you share your code?

    This is the code I use, obviously it needs a revamp:

    DTE2 _applicationObject = (DTE2)Package.GetGlobalService(typeof(SDTE));

                UIHierarchy solutionExplorer = _applicationObject.ToolWindows.SolutionExplorer;
                var items = solutionExplorer.SelectedItems as Array;

                if (items == null || items.Length != 2)
                {
                    return;
                }

                String strFile1 = String.Empty;
                UIHierarchyItem item1 = items.GetValue(0) as UIHierarchyItem;
                item1.UIHierarchyItems.
                ProjectItem prj1 = item1.Object as ProjectItem;
                if (prj1.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                {
                    String strProjectFolder = String.Empty;
                    try
                    {
                        strProjectFolder = prj1.ContainingProject.Properties.Item("FullPath").Value.ToString();
                    }
                    catch (ArgumentException)
                    {
                        // don't know why it didn't work, but if it does throw, try alternative
                        strProjectFolder = Path.GetDirectoryName(prj1.ContainingProject.FullName);
                    }
                    strFile1 = Path.Combine(strProjectFolder, item1.Name);
                }

                String strFile2 = String.Empty;
                UIHierarchyItem item2 = items.GetValue(1) as UIHierarchyItem;
                ProjectItem prj2 = item2.Object as ProjectItem;
                if (prj2.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                {
                    String strProjectFolder = String.Empty;
                    try
                    {
                        strProjectFolder = prj2.ContainingProject.Properties.Item("FullPath").Value.ToString();
                    }
                    catch (ArgumentException)
                    {
                        // don't know why it didn't work, but if it does throw, try alternative
                        strProjectFolder = Path.GetDirectoryName(prj2.ContainingProject.FullName);
                    }
                    strFile2 = Path.Combine(strProjectFolder, item2.Name);
                }

                RegistryKey keyBeyondCompare = Registry.CurrentUser.OpenSubKey(@"Software\Scooter Software\Beyond Compare 3");
                String strExePath = (String)keyBeyondCompare.GetValue("ExePath");

                if (File.Exists(strExePath) && File.Exists(strFile1) && File.Exists(strFile2))
                {
                    ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName = strExePath;
                    psi.Arguments = String.Format("\"{0}\" \"{1}\"", strFile1, strFile2);
                    if (System.Diagnostics.Process.Start(psi) == null)
                    {
                        MessageBox.Show("Could not launch compare.", "Cannot launch compare", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }

    Thank you for any help or reference..

    Vaso Elias

     

     

    • Edited by Vaso Elias Monday, April 19, 2010 10:52 AM ..name changed
    Monday, April 19, 2010 10:25 AM

Answers

  • Halo, Vaso Elias

    If you prefer the SDK method, you could consider IVsMonitorSelection interface, but this is a bit complex.

    Actually, there is a easier way in Automation, because the projectItem have a Properties property, you could get the path of the selected item through the Properties property.

    See the following code based on your code, it is much easier.

                        UIHierarchy solutionExplorer = _applicationObject.ToolWindows.SolutionExplorer;

                        var items = solutionExplorer.SelectedItems as Array;

                        if (items == null || items.Length != 2)

                        {

                            return;

                        }

                        String strFile1 = String.Empty;

                        UIHierarchyItem item1 = items.GetValue(0) as UIHierarchyItem;

                        foreach (UIHierarchyItem hierarchyItem in items)

                        {

                            ProjectItem prjItem = hierarchyItem.Object as ProjectItem;

                            string prjPath = prjItem.Properties.Item("FullPath").Value.ToString();

                        }

    Thanks

    Chao

    • Marked as answer by Vaso Elias Wednesday, April 21, 2010 9:40 AM
    • Edited by Chao KuoModerator Thursday, April 22, 2010 3:16 AM lost some words
    Wednesday, April 21, 2010 8:11 AM

All replies

  • Halo, Vaso Elias

    If you prefer the SDK method, you could consider IVsMonitorSelection interface, but this is a bit complex.

    Actually, there is a easier way in Automation, because the projectItem have a Properties property, you could get the path of the selected item through the Properties property.

    See the following code based on your code, it is much easier.

                        UIHierarchy solutionExplorer = _applicationObject.ToolWindows.SolutionExplorer;

                        var items = solutionExplorer.SelectedItems as Array;

                        if (items == null || items.Length != 2)

                        {

                            return;

                        }

                        String strFile1 = String.Empty;

                        UIHierarchyItem item1 = items.GetValue(0) as UIHierarchyItem;

                        foreach (UIHierarchyItem hierarchyItem in items)

                        {

                            ProjectItem prjItem = hierarchyItem.Object as ProjectItem;

                            string prjPath = prjItem.Properties.Item("FullPath").Value.ToString();

                        }

    Thanks

    Chao

    • Marked as answer by Vaso Elias Wednesday, April 21, 2010 9:40 AM
    • Edited by Chao KuoModerator Thursday, April 22, 2010 3:16 AM lost some words
    Wednesday, April 21, 2010 8:11 AM
  • Hi Chao,

    I am afraid your code works just perfectly :-) he he I couln't find anywhere any easy way like you have just posted..

    I will try to include there also other options so you can pick up your own file comparison tool and if it works I will publish it in the Gallery.

    Thank you very much.

    Vaclav.

    Wednesday, April 21, 2010 9:44 AM
  • I am sorry I miss some words in my last reply, I have modified it.
    Thursday, April 22, 2010 3:17 AM
  • The question and answer were exactly what I needed.  Thank you!
    Michael Leonard
    Tuesday, June 08, 2010 4:08 PM
  • This is what I need, too. Thanks!
    Thursday, July 14, 2011 9:19 PM
  • Sir, You are welcome.
    Please unmark it if it does not help, and mark it if it helps.
    Tuesday, August 16, 2011 7:35 AM