How do you open up a File GDB and read out a simple feature class?

The snippet for retreiving a shapefile from a regular folder on disk works, and I suppose I can use the same code but with a different kind of workspace.

''' <summary>
''' Get the FeatureClass from a Shapefile on disk (hard drive).
''' </summary>
''' <param name="string_ShapefileDirectory">A System.String that is the directory where the shapefile is located. Example: "C:\data\USA"</param>
''' <param name="string_ShapefileName">A System.String that is the shapefile name. Note: the shapefile extension's (.shp, .shx, .dbf, etc.) is not provided! Example: "States"</param>
''' <returns>An IFeatureClass interface. Nothing (VB.NET) or null (C#) is returned if unsuccessful.</returns>
''' <remarks></remarks>
Public Function GetFeatureClassFromShapefileOnDisk(ByVal string_ShapefileDirectory As System.String, ByVal string_ShapefileName As System.String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

  Dim directoryInfo_check As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(string_ShapefileDirectory)
  If directoryInfo_check.Exists Then

    'We have a valid directory, proceed

    Dim fileInfo_check As System.IO.FileInfo = New System.IO.FileInfo(string_ShapefileDirectory + "\" + string_ShapefileName + ".shp")
    If fileInfo_check.Exists Then

      'We have a valid shapefile, proceed

      Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass
      Dim workspace As ESRI.ArcGIS.Geodatabase.IWorkspace = workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0)
      Dim featureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = CType(workspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace) ' Explict Cast
      Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = featureWorkspace.OpenFeatureClass(string_ShapefileName)

      Return featureClass

      'Not valid shapefile
      Return Nothing
    End If


    ' Not valid directory
    Return Nothing

  End If

End Function

1 Answer 1


Assuming your project is setup correctly, with all references added and compiles without errors. Using Visual Studio Express 2013, ArcGIS 10.3 and targeting .Net framework 3.5

Also ensure you add ArcObjects Library References to:

  • DataSourcesGDB
  • GeoDatabase
  • Carto
Public Sub New()
On Error GoTo Trap

    Dim sPathFGDB As String
    Dim sFCName As String
    Dim pFC As IFeatureClass
    Dim pWorkSpace As IFeatureWorkspace
    Dim pWorkspaceFactory As IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory

    sPathFGDB = InputBox("Enter path to FGDB:", "Path to FGDB", "C:\Users\jakub.sisak\Documents\ArcGIS\Default.gdb") 'enter a valid path including the ".gdb" extension
    pWorkSpace = pWorkspaceFactory.OpenFromFile(sPathFGDB, 0)    
    sFCName = InputBox("Enter Feature Class Name:", "Feature Class Name", "Closure_Plan_GA_Footprint_DE") 'enter a valid feature class name
    pFC = pWorkSpace.OpenFeatureClass(sFCName)

    Dim pLayer As IFeatureLayer
    pLayer = New FeatureLayer
    pLayer.FeatureClass = pFC
    pLayer.Name = pFC.AliasName

    Dim pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument = My.ArcMap.Document    
    pMxDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, pLayer, Nothing)

Exit Sub
    MsgBox(Err.Number & ": " & Err.Description)

End Sub
  • Just out of preference for code that is closer to C# and more .NET friendly, I would replace replace the On Error GoToTrap and Trap with a Try Catch Finally block. Also, I would replace the MsgBox with the Forms MessageBox and InputBox with a custom form (since there isn't a C# InputBox equivalent. It'll all work, but I have seen more examples using more .NET libraries as opposed to the VB6 (VisualBasic.dll) libraries. I just find it easier when writing code in both VB.NET and C# to translate when I exclude VB6 items.
    – Branco
    Commented May 21, 2015 at 16:58
  • Thanks for your input. OP is asking about VB.Net not C#. This procedure illustrates the mechanics of getting then adding a FGDB Feature to Map which is the focus here. Commented May 21, 2015 at 17:12
  • Yes which is why I posted as a comment. It is better to adapt to C# style VB.NET instead of VB6 since VB6 is a legacy style.
    – Branco
    Commented May 21, 2015 at 18:16
  • @Jakub thank you very much! That was quite easy! I was too fixated on the fileInfo existance check, but a path to a feature class in a gdb is kind of hard to manage. I used IWorkspace2.NameExist() instead. Thanks for also adding how to add the layer to a map!
    – bvh9000
    Commented May 22, 2015 at 13:36
  • @Branco I agree, I don't know a lot of VB6, I'm a C# guy who's working on a vb project for a change so I also stick to the .NET framework
    – bvh9000
    Commented May 22, 2015 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.