Type factory_type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory"); IWorkspaceFactory workspace_factory = (IWorkspaceFactory)Activator.CreateInstance(factory_type); IFeatureWorkspace workspace = (IFeatureWorkspace)workspace_factory.OpenFromFile(@"C:\Shared\sample\", 0); IFeatureClass feature_class = workspace.OpenFeatureClass(System.IO.Path.GetFileName(sample); ESRI.ArcGIS.Carto.IFeatureLayer feature_layer = new FeatureLayerClass(); feature_layer.FeatureClass = (IFeatureClass)feature_class; default_map.AddLayer((ILayer)feature_layer);
// Variables string fullpathToShapefile = "C:\Shared\sample\sample.shp" string shp_path = Path.GetDirectoryName(fullpathToShapefile); // This will be "C:\Shared\sample" string shp_name = Path.GetFileNameWithoutExtension(fullpathToShapefile); // this will be "sample" Type factory_type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory"); IWorkspaceFactory ShpWksFact = (IWorkspaceFactory)Activator.CreateInstance(factory_type); IFeatureWorkspace FeatWks = (IFeatureWorkspace)ShpWksFact.OpenFromFile(shp_path, 0); IFeatureClass FeatClass = FeatWks.OpenFeatureClass(shp_name); IFeatureLayer FeatLayer = new FeatureLayerClass(); FeatLayer.FeatureClass = FeatClass;
Which statement throws the exception? What is the exact string value that is being evaluated with your expression System.IO.Path.GetFileName(sample)? Does it include the .shp extension? If so, try without the .shp extension. The below code snippet states to specify the shapefile name without the extension. Try both ways as a test and make sure you are not missing a close parenthesis:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Get_FeatureClass_From_Sh...
Not sure if you ever got this working but since your code helped me get adding shapefiles... I figured I would post how I did it.// Variables string fullpathToShapefile = "C:\Shared\sample\sample.shp" string shp_path = Path.GetDirectoryName(fullpathToShapefile); // This will be "C:\Shared\sample" string shp_name = Path.GetFileNameWithoutExtension(fullpathToShapefile); // this will be "sample" Type factory_type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory"); IWorkspaceFactory ShpWksFact = (IWorkspaceFactory)Activator.CreateInstance(factory_type); IFeatureWorkspace FeatWks = (IFeatureWorkspace)ShpWksFact.OpenFromFile(shp_path, 0); IFeatureClass FeatClass = FeatWks.OpenFeatureClass(shp_name); IFeatureLayer FeatLayer = new FeatureLayerClass(); FeatLayer.FeatureClass = FeatClass;
Hope it helps