Along with my last few posts, I received an Error when trying to open shapefiles with Arcobjects. Thought I would post the problem incase someone else ran into it.
Here is the code to open shapefiles with Arcobjects.
IWorkspaceFactory workspaceFactoryShape = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); //creates a shape factory to pull the shape and create the feature class.
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactoryShape.OpenFromFile(System.IO.Path.GetDirectoryName(file), 0); //Opens the Shape file.
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(file)); //opens the featurclass from the shapefile
Two things to point out.
- The openfromFile method will throw the error: HRESULT: 0x80040258 if you don’t get the directory in which the shape files are located. Its not looking for the actual .shp file. Its looking for where the shape files reside.
- The OpenFeatureclass method is where you give just the file name of the shape file. Don’t include the .shp extenstion nor the file path. Remember it already has the file path I stated in the first point.
Hope this helps someone out! I know I struggled with it for about 2 hours…
If you liked this post, please be sure to subscribe to my RSS Feed.