locked
Identify if the selected project item is a Folder or a File RRS feed

  • Question

  • Hi,

    I am new to VS extensibility, I am learning Integration using VS Packages.

    I have eclipse plugin development background. In eclipse IDE project, folders, files types are defined in separate classes. We can identify if the selected item under a project is file or folder by testing its class-type (instance of).

    I searched for a similar capability in visual studio and i understood the availability if IVsHeirarchy and we have to iterate it and use "itemId" to get the ProjectItem. But i really could not find how to test if the project item is a folder or a file.

    I found usage some properties like "__VSHPROPID.VSHPROPID_Expandable" and "__VSHPROPID2.VSHPROPID_Container". But is it the right way? Because i read that in some projets we may also have "Nested" files. So I am really not sure how to check if the selected item is a folder or a file.

    Thanks
    Karthik



    Wednesday, February 4, 2009 12:56 PM

Answers

  • Different project systems will have different underlying classes/objects that implement the requisite interfaces for file and folder nodes in a project hierarchy. Project hierarchies can be implemented with managed or native code, so testing "class-types" won't work.

    The best way to test this scenario in a "generic" fashion, would be to test the filename associated with the individual ProjectItems to see if it corresponds to a valid directory path.

    For example, the following macros illustrates how to do this using the generic automation support built into most project types.


    Function GetMiscDumpPane() As OutputWindowPane
       Dim ow As OutputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object
       Dim pane As OutputWindowPane
       Try
         
    pane = ow.OutputWindowPanes.Item("MiscDumpPane")
       Catch ex As Exception
          pane = ow.OutputWindowPanes.Add(
    "MiscDumpPane")
       End Try
       Return pane
    End Function

    Sub WalkProjectItems(ByVal items As ProjectItems, ByVal outputPane As OutputWindowPane)
       For Each projItem As ProjectItem In items
          If Not projItem.ProjectItems Is Nothing Then
             WalkProjectItems(projItem.ProjectItems, outputPane)
          End If
          If Directory.Exists(projItem.FileNames(1)) Then
             outputPane.OutputString(projItem.FileNames(1) & vbCr)
          End If
       Next
    End Sub

    Sub DumpFolderNames()
       Dim outputPane As OutputWindowPane = GetMiscDumpPane()
       For Each proj As Project In DTE.Solution.Projects
          outputPane.OutputString(
    "=================" & vbCr)
          outputPane.OutputString(proj.Name & vbCr)
          outputPane.OutputString(
    "=================" & vbCr)
          WalkProjectItems(proj.ProjectItems, outputPane)
       Next
    End Sub

    If you are using a specific project type (say VB or C++), you can also use the ProjectItem.Kind property to differentiate between the different node types. The ProjectItem.Kind property is a guid string that uniquely identifies the type of node, and you could also use this. But the guid value varies from project type to project type.

    Sincerely,


    Ed Dore
    Wednesday, February 4, 2009 9:43 PM

All replies

  • Very interesting question!
    I need an answer about it too!
    - Matteo Garzulino -
    Wednesday, February 4, 2009 1:04 PM
  • Different project systems will have different underlying classes/objects that implement the requisite interfaces for file and folder nodes in a project hierarchy. Project hierarchies can be implemented with managed or native code, so testing "class-types" won't work.

    The best way to test this scenario in a "generic" fashion, would be to test the filename associated with the individual ProjectItems to see if it corresponds to a valid directory path.

    For example, the following macros illustrates how to do this using the generic automation support built into most project types.


    Function GetMiscDumpPane() As OutputWindowPane
       Dim ow As OutputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object
       Dim pane As OutputWindowPane
       Try
         
    pane = ow.OutputWindowPanes.Item("MiscDumpPane")
       Catch ex As Exception
          pane = ow.OutputWindowPanes.Add(
    "MiscDumpPane")
       End Try
       Return pane
    End Function

    Sub WalkProjectItems(ByVal items As ProjectItems, ByVal outputPane As OutputWindowPane)
       For Each projItem As ProjectItem In items
          If Not projItem.ProjectItems Is Nothing Then
             WalkProjectItems(projItem.ProjectItems, outputPane)
          End If
          If Directory.Exists(projItem.FileNames(1)) Then
             outputPane.OutputString(projItem.FileNames(1) & vbCr)
          End If
       Next
    End Sub

    Sub DumpFolderNames()
       Dim outputPane As OutputWindowPane = GetMiscDumpPane()
       For Each proj As Project In DTE.Solution.Projects
          outputPane.OutputString(
    "=================" & vbCr)
          outputPane.OutputString(proj.Name & vbCr)
          outputPane.OutputString(
    "=================" & vbCr)
          WalkProjectItems(proj.ProjectItems, outputPane)
       Next
    End Sub

    If you are using a specific project type (say VB or C++), you can also use the ProjectItem.Kind property to differentiate between the different node types. The ProjectItem.Kind property is a guid string that uniquely identifies the type of node, and you could also use this. But the guid value varies from project type to project type.

    Sincerely,


    Ed Dore
    Wednesday, February 4, 2009 9:43 PM
  • Hi Ed,

    Thank you so much for your code sample.
    It helped me a lot even though i am using c#.
    I got the idea of your approach.

    Thanks Again
    Karthik


    Thursday, February 5, 2009 6:31 AM
  • Hi Ed,

    Thank you so much for your code sample.
    It helped me a lot even though i am using c#.
    I got the idea of your approach.

    Thanks Again
    Karthik
    Thursday, February 5, 2009 6:31 AM