I have a folder contain 5 shapefiles.

I want to develop C# code for adding the 5 shapefiles with single button click.

I have created code for adding a single shapefile but how can I add multiple shapefiles?

ESRI.ArcGIS.ArcMapUI.IMxDocument mxd = ArcMap.Application.Document as ESRI.ArcGIS.ArcMapUI.IMxDocument;
ESRI.ArcGIS.Carto.IMap map = mxd.FocusMap;
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory();
ESRI.ArcGIS.Geodatabase.IWorkspace ws = wsf.OpenFromFile(@"D:\ARC OBJECTS\Kothagudem Attribute Data", ArcMap.Application.hWnd);
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace fws = ws as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
ESRI.ArcGIS.Geodatabase.IFeatureClass fcls = fws.OpenFeatureClass("Agriculture.shp");
ESRI.ArcGIS.Carto.IFeatureLayer lay = new ESRI.ArcGIS.Carto.FeatureLayerClass();
lay.FeatureClass = fcls;
lay.Name = fcls.AliasName;
  • 1
    What have you tried? In what way did it fail? It seems like you want us to teach looping in C#, but that isn't a GIS task.
    – Vince
    Jan 22, 2018 at 21:43
  • Have a look at IGPUtilities.OpenFeatureLayerFromString stops you having to write so much code.
    – Hornbydd
    Jan 22, 2018 at 22:49

1 Answer 1


You can loop over feature classes in an IWorkspace by using

IEnumDataset eds = ws.Datasets[esriDatasetType.esriDTFeatureClass]

You can then write a function like this to access those feature classes easily

IEnumerable<IFeatureClass> getFeatureClasses(IEnumDataset eds)
    IDataset ds;
    while ((ds = eds.Next()) != null)
        yield return ds as IFeatureClass;

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.