1. 程式人生 > >AE+C#通過名稱來刪除要素類

AE+C#通過名稱來刪除要素類

  1. //注: 在程式執行中往往有些要素類因為出錯而無法刪除,這時IDataset.Delete這個方法則無法成功,可以用輕量級的名稱物件來完成這個任務。以下為C#+AE10原始碼。
  2.   方法一:
  3. //刪除要素類
  4.         publicstaticbool DeleteFeatureDataset(IWorkspace ws, string name)  
  5.         {  
  6.             if (ws == null || string.IsNullOrEmpty(name))  
  7.             {  
  8.                 ErrMsg = "工作空間或要素類名稱不能為空!"
    ;  
  9.                 returnfalse;  
  10.             }  
  11.             IFeatureDataset pFeaDataSet;  
  12.             IEnumDatasetName pEnumDatasetName;  
  13.             IFeatureWorkspace pFeaWorkspace;  
  14.             IDatasetName pDatasetName;  
  15.             try
  16.             {  
  17.                 pFeaWorkspace = ws as
     IFeatureWorkspace;  
  18.                 pEnumDatasetName = ws.get_DatasetNames(esriDatasetType.esriDTFeatureClass ^ esriDatasetType.esriDTFeatureDataset);  
  19.                 pEnumDatasetName.Reset();  
  20.                 pDatasetName = pEnumDatasetName.Next();  
  21.                 while (pDatasetName != null
    )  
  22.                 {  
  23.                     if (pDatasetName.Type == esriDatasetType.esriDTFeatureDataset)  
  24.                     {  
  25.                         //如果是要素集,則對要素集內的要素類進行查詢
  26.                         IEnumDatasetName pEnumFcName = (pDatasetName as IFeatureDatasetName).FeatureClassNames;  
  27.                         IDatasetName pFcName = pEnumFcName.Next();  
  28.                         while (pFcName != null)  
  29.                         {  
  30.                             if (pFcName.Name.IndexOf(name) >= 0)  
  31.                             {  
  32.                                 DeleteByName(pFeaWorkspace, pFcName);  
  33.                                 returntrue;  
  34.                             }  
  35.                             pFcName = pEnumFcName.Next();  
  36.                         }  
  37.                     }  
  38.                     else
  39.                     {  
  40.                         if (pDatasetName.Name.IndexOf(name) >= 0)  
  41.                         {  
  42.                             DeleteByName(pFeaWorkspace, pDatasetName);  
  43.                             returntrue;  
  44.                         }  
  45.                     }  
  46.                     pDatasetName = pEnumDatasetName.Next();  
  47.                 }  
  48.                 returnfalse;  
  49.             }  
  50.             catch (Exception ex)  
  51.             {  
  52.                 ErrMsg = ex.Message;  
  53.                 returnfalse;  
  54.             }  
  55.         }  
  56.         //刪除名稱物件
  57.         publicstaticvoid DeleteByName(IFeatureWorkspace pFeaWorkspace, IDatasetName pDatasetName)  
  58.         {  
  59.             IFeatureWorkspaceManage pWorkspaceManager = pFeaWorkspace as IFeatureWorkspaceManage;  
  60.             pWorkspaceManager.DeleteByName(pDatasetName);  
  61.         }  
  62. 方法二:
  63. 呼叫GP工具,datamanagement下的delete工具
  64.         ESRI.ArcGIS.DataManagementTools.Delete deleteTool = new ESRI.ArcGIS.DataManagementTools.Delete();
            deleteTool.in_data = otherPara[0];
            geoProcessor.Execute(deleteTool, null);