IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();//打开shapefile工作空间 openFileDialog1.Filter = ("shapefile文件(*.shp)|*.shp");//文件过滤 openFileDialog1.InitialDirectory=@"C:\Program Files\ARCGIS\GIS-Data";//默认路径吧 openFileDialog1.Multiselect = false;//只能单选 DialogResult pDialogResult = openFileDialog1.ShowDialog(); if (pDialogResult != DialogResult.OK) return;//如果没点击确认 ,返回 string pPath = openFileDialog1.FileName;//获得文件的路径 string pFloder = Path.GetDirectoryName(pPath);//得到文件所在的文件夹位置 string pFileName = Path.GetFileName(pPath);//得到文件名 IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFloder, 0);//打开工作空间 IFeatureWorkspace pFeatureWokkspace = pWorkspace as IFeatureWorkspace;//转换工作空间为要素工作空间 IFeatureClass pFC = pFeatureWokkspace.OpenFeatureClass(pFileName);//打开要素类 IFeatureLayer pFLayer = new FeatureLayerClass(); pFLayer.FeatureClass = pFC;//设置图层的featureclass为上面打开的文件 pFLayer.Name = pFC.AliasName;//设置图层的名称 ILayer pLayer = pFLayer as ILayer;//转换featurelayer为layer IMap pMap = axMapControl1.Map; pMap.AddLayer(pLayer);//添加图层 axMapControl1.ActiveView.Refresh();//刷新 |
来源:https://www.cnblogs.com/zgl-gis/p/11975611.html