private void AddShapeFile() { // Create a new ShapefileWorkspaceFactory object and // open a shapefile folder - the path works with standard 9.3 installation IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace) workspaceFactory.OpenFromFile( "C:\\Program Files\\ArcGIS\\DeveloperKit\\SamplesNET\\data\\Y2000HurricaneData", 0); // Create a new FeatureLayer and assign a shapefile to it IFeatureLayer featureLayer = new FeatureLayerClass(); featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass("2000_hrcn"); ILayer layer = (ILayer)featureLayer; layer.Name = featureLayer.FeatureClass.AliasName; // Add the Layer to the focus map ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = (ESRI.ArcGIS.ArcMapUI.IMxDocument)(m_application.Document); IMap map = mxDocument.FocusMap; map.AddLayer(layer); }
private void btnOpenShapefile_Click(object sender, EventArgs e) { ESRI.ArcGIS.Catalog.IGxObjectFilterCollection pGxFilter; //establish a collection of filters ESRI.ArcGIS.Catalog.GxFilterShapefiles pfilter2; //create a filter Boolean notanythingselected; ESRI.ArcGIS.Catalog.IGxObject gxObj; //declare an instance of GxObjects ESRI.ArcGIS.CatalogUI.IGxDialog pGxDia; //declare an instance of GxDialogs pfilter2 = new ESRI.ArcGIS.Catalog.GxFilterShapefiles(); //create a filter instance pGxDia = new GxDialogClass(); //new dialog box object pGxDia.Title = "Choose a Shapefile"; pGxFilter = (ESRI.ArcGIS.Catalog.IGxObjectFilterCollection)pGxDia; pGxFilter.AddFilter(pfilter2, true); ESRI.ArcGIS.Catalog.IEnumGxObject gxEnum = null; notanythingselected = pGxDia.DoModalOpen(this.Handle.ToInt32(), out gxEnum); if (notanythingselected == false) { return; } else { gxEnum.Reset(); gxObj = gxEnum.Next(); ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wksFact; wksFact = new ShapefileWorkspaceFactory(); ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featWrk; featWrk = (IFeatureWorkspace)wksFact.OpenFromFile(gxObj.Parent.FullName, 0); ESRI.ArcGIS.Geodatabase.IFeatureClass fClass; fClass = featWrk.OpenFeatureClass(gxObj.Name); ESRI.ArcGIS.Carto.IFeatureLayer lyr; lyr = new FeatureLayer(); lyr.FeatureClass = fClass; lyr.Name = gxObj.Name; ESRI.ArcGIS.ArcMapUI.IMxDocument mxDoc = (ESRI.ArcGIS.ArcMapUI.IMxDocument)(m_application.Document); //ERROR IS HERE AT M_application IMap map = mxDoc.FocusMap; map.AddLayer(lyr);
Solved! Go to Solution.
First of all, is this an ArcGIS desktop or engine application?
Secondly, are you developing an addin or extension code?
Thirdly, how is the code called from the application? Did you create a command or menu? Is it an event handler?