c# AE 通过名称删除要素类

转载:http://blog.csdn.net/lysc_forever/article/details/7790219#

 1 //注: 在程序运行中往往有些要素类因为出错而无法删除,这时IDataset.Delete这个方法则无法成功,可以用轻量级的名称对象来完成这个任务。以下为C#+AE10源码。
 2 
 3 //删除要素类
 4         public static bool DeleteFeatureDataset(IWorkspace ws, string name)
 5         {
 6             if (ws == null || string.IsNullOrEmpty(name))
 7             {
 8                 ErrMsg = "工作空间或要素类名称不能为空!";
 9                 return false;
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                                 return true;
34                             }
35                             pFcName = pEnumFcName.Next();
36                         }
37                     }
38                     else
39                     {
40                         if (pDatasetName.Name.IndexOf(name) >= 0)
41                         {
42                             DeleteByName(pFeaWorkspace, pDatasetName);
43                             return true;
44                         }
45                     }
46                     pDatasetName = pEnumDatasetName.Next();
47                 }
48                 return false;
49             }
50             catch (Exception ex)
51             {
52                 ErrMsg = ex.Message;
53                 return false;
54             }
55         }
56         //删除名称对象
57         public static void DeleteByName(IFeatureWorkspace pFeaWorkspace, IDatasetName pDatasetName)
58         {
59             IFeatureWorkspaceManage pWorkspaceManager = pFeaWorkspace as IFeatureWorkspaceManage;
60             pWorkspaceManager.DeleteByName(pDatasetName);
61         }

 

posted @ 2017-09-01 17:16  marvelousone  阅读(517)  评论(0编辑  收藏  举报