/// <summary>
/// 获取GDB下所有图层返回List
/// </summary>
/// <param name="gdbPath"></param>
/// <returns></returns>
public static List<string> getGdbFeatures(string gdbPath)
{
using (ComReleaser comReleaser = new ComReleaser())
{
FileGDBWorkspaceFactoryClass pFileGDBWorkspaceFactoryClass = new FileGDBWorkspaceFactoryClass();
comReleaser.ManageLifetime(pFileGDBWorkspaceFactoryClass);
IWorkspace pFeatureWorkspace = pFileGDBWorkspaceFactoryClass.OpenFromFile(gdbPath, 0);
comReleaser.ManageLifetime(pFeatureWorkspace);
IFeatureWorkspace Workspace = pFeatureWorkspace as IFeatureWorkspace;
comReleaser.ManageLifetime(Workspace);
IEnumDataset pEnumDatasets = pFeatureWorkspace.get_Datasets(esriDatasetType.esriDTAny) as IEnumDataset;
comReleaser.ManageLifetime(pEnumDatasets);
IDataset pDataset = pEnumDatasets.Next();
comReleaser.ManageLifetime(pDataset);
List<string> listFeaClass = new List<string>();
while (pDataset != null)
{
if (pDataset.Type == esriDatasetType.esriDTFeatureClass)
{
listFeaClass.Add(pDataset.Name);
}
else if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
{
IEnumDataset pESubDataset = pDataset.Subsets;
IDataset pSubDataset = pESubDataset.Next();
while (pSubDataset != null)
{
listFeaClass.Add(pSubDataset.Name);
pSubDataset = pESubDataset.Next();
}
}
pDataset = pEnumDatasets.Next();
}
return listFeaClass;
}
}
/// <summary>
/// 获取图层字段信息返回dataTable
/// </summary>
/// <param name="gdbPath"></param>
/// <param name="featureClassName"></param>
/// <returns></returns>
public static DataTable getFieldDataTable (string gdbPath, string featureClassName)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Field_Name",typeof(string));
dataTable.Columns.Add("Field_Type", typeof(string));
dataTable.Columns.Add("Field_Length", typeof(int));
dataTable.Columns.Add("Field_AliasName", typeof(string));
using (ComReleaser comReleaser = new ComReleaser())
{
FileGDBWorkspaceFactoryClass workspaceFactory = new FileGDBWorkspaceFactoryClass();
comReleaser.ManageLifetime(workspaceFactory);
IWorkspace workspace = workspaceFactory.OpenFromFile(gdbPath, 0);
comReleaser.ManageLifetime(workspace);
IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
comReleaser.ManageLifetime(featureWorkspace);
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(featureClassName);
comReleaser.ManageLifetime(featureClass);
for (int i = 0; i < featureClass.Fields.FieldCount; i++)
{
IField field = featureClass.Fields.get_Field(i);
DataRow row = dataTable.NewRow();
row[0] = field.Name;
row[1] = field.Type.ToString();
row[2] = field.Length;
row[3] = field.AliasName;
dataTable.Rows.Add(row);
comReleaser.ManageLifetime(field);
}
}
return dataTable;
}
C#获取GDB下图层名称及字段信息
最新推荐文章于 2024-08-31 14:48:17 发布