// 转换 private void btnTransfer_Click(object sender, EventArgs e) { this.lblInfo.Text = "开始转换..."; string sourceLayerName = this.tbxLayerName.Text; string targetPath = this.tbxShpPath.Text; FindAllFiles(this.tbxMdbPath.Text); foreach (string mdbFile in fileList) { string sourcePath = System.IO.Path.GetDirectoryName(mdbFile); string sourceFileName = System.IO.Path.GetFileName(mdbFile); bool bb = ExportShp(sourcePath, sourceFileName, sourceLayerName, targetPath); if (bb == false) { MessageBox.Show("检查mdb文件是否已经打开被占用...", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } this.lblInfo.Text = "转换完成!"; MessageBox.Show("转换完成!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private bool ExportShp(string sourcePath, string sourceFileName, string layerName, string targetPath) { // Create a name object for the source (shapefile) workspace and open it. // 打开源mdb文件 IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass { WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory", PathName = sourcePath + "\\" + sourceFileName }; IName sourceWorkspaceIName = null; IWorkspace sourceWorkspace = null; try { sourceWorkspaceIName = (IName)sourceWorkspaceName; sourceWorkspace = (IWorkspace)sourceWorkspaceIName.Open(); } catch (Exception) { return false; } // Create a name object for the target (file GDB) workspace and open it. // 创建输出空间 IWorkspaceName targetWorkspaceName = new WorkspaceNameClass { WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory", PathName = targetPath }; IName targetWorkspaceIName = (IName)targetWorkspaceName; IWorkspace targetWorkspace = (IWorkspace)targetWorkspaceIName.Open(); // Create a name object for the source dataset. // 创建源数据集名称 IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.Name = layerName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; // Create a name object for the target dataset. // 创建目标数据集名称 IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = sourceFileName.Substring(0, sourceFileName.IndexOf(".")) + layerName; targetDatasetName.WorkspaceName = targetWorkspaceName; // Open source feature class to get field definitions. // 打开源数据集字段定义 IName sourceName = null; IFeatureClass sourceFeatureClass = null; try { sourceName = (IName)sourceFeatureClassName; sourceFeatureClass = (IFeatureClass)sourceName.Open(); } catch (Exception ex) { return true; } // Create the objects and references necessary for field validation. // 创建字段验证 IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFields = sourceFeatureClass.Fields; IFields targetFields = null; IEnumFieldError enumFieldError = null; // Set the required properties for the IFieldChecker interface. // 设置字段验证接口的属性 fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; // Validate the fields and check for errors. // 验证字段 检查错误 fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError != null) { // Handle the errors in a way appropriate to your application. MessageBox.Show("Errors were encountered during field validation."); } // Find the shape field. // 找shp字段 String shapeFieldName = sourceFeatureClass.ShapeFieldName; int shapeFieldIndex = sourceFeatureClass.FindField(shapeFieldName); IField shapeField = sourceFields.get_Field(shapeFieldIndex); // Get the geometry definition from the shape field and clone it. // 克隆几何字段 IGeometryDef geometryDef = shapeField.GeometryDef; IClone geometryDefClone = (IClone)geometryDef; IClone targetGeometryDefClone = geometryDefClone.Clone(); IGeometryDef targetGeometryDef = (IGeometryDef)targetGeometryDefClone; // Create a query filter // 创建过滤器 IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; // Create the converter and run the conversion. // 创建转换器 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass( sourceFeatureClassName, queryFilter, null, targetFeatureClassName, targetGeometryDef, targetFields, "", 1000, 0); // Check for errors. // 检查错误 IInvalidObjectInfo invalidObjectInfo = null; enumInvalidObject.Reset(); while ((invalidObjectInfo = enumInvalidObject.Next()) != null) { MessageBox.Show("Errors occurred for the following feature: " + invalidObjectInfo.InvalidObjectID.ToString()); } return true; }
// 遍历文件夹及子文件夹 List<string> fileList = new List<string>(); private void FindAllFiles(string path) { DirectoryInfo drInfo = new DirectoryInfo(path); // 获取当前目录下.mdb文件, 添加至fileList FileInfo[] fi = drInfo.GetFiles(); foreach (FileInfo item in fi) { if (item.Extension==".mdb"||item.Extension==".MDB") { fileList.Add(item.FullName); } } // 获取当前目录下所有子文件夹, 并递归遍历所有子文件夹 DirectoryInfo[] subDirs = drInfo.GetDirectories(); foreach (DirectoryInfo subDir in subDirs) { string subFile = subDir.FullName + "\\"; FindAllFiles(subFile); } }
其中ExportShp函数来源于: http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#/Converting_and_transferring_data/0001000003rp000000/
原文:https://www.cnblogs.com/lihuijie/p/11375759.html