Get Feature Class name from MXD layers? (.NET)

368
3
12-09-2010 04:12 AM
TonyCollins
Occasional Contributor
Hi,

Could someone tell me how to retrieve the Feature Class name for each layer in an MXD?

I tried this:

MapControlClass mapControl = new ESRI.ArcGIS.MapControl.MapControlClass();
mapControl.LoadMxFile(filename,null,null);

ESRI.ArcGIS.Carto.IFeatureLayer layer =
(ESRI.ArcGIS.Carto.IFeatureLayer)mapControl.get_Layer(1) as ESRI.ArcGIS.Carto.IFeatureLayer;

But the Feature Class property is null?
0 Kudos
3 Replies
JeffreyHamblin
New Contributor III
get_Layer() returns an ILayer. So you should use that first, then test if it is an IFeatureLayer.

I haven't worked with MapControl, but I think something like this should work:

for (int i = 0; i < mapControl.LayerCount; i++)
{
  ILayer layer = mapControl.get_Layer(i) as ILayer;
  if (!(layer is IFeatureLayer))
    continue;
  IFeatureLayer featureLayer = (IFeatureLayer)layer;
  string fName = featureLayer.FeatureClass.AliasName;
  // do something with fName
}


-Jeff
0 Kudos
Venkata_RaoTammineni
Occasional Contributor
Hi, 

Could someone tell me how to retrieve the Feature Class name for each layer in an MXD? 

I tried this: 

MapControlClass mapControl = new ESRI.ArcGIS.MapControl.MapControlClass();
mapControl.LoadMxFile(filename,null,null);

ESRI.ArcGIS.Carto.IFeatureLayer layer =
(ESRI.ArcGIS.Carto.IFeatureLayer)mapControl.get_Layer(1) as ESRI.ArcGIS.Carto.IFeatureLayer;


But the Feature Class property is null?



ESRI.ArcGIS.Carto.IFeatureLayer ptestLayer = mapcontl.FocusMap.get_Layer(1).Name;

Thanks and Regards,

Venkat
0 Kudos
Ariharan
New Contributor
Am also having same problem, If i use some class instead of AxMapControl, getting error as null value in featureclass.
Have anyone got the answer?
0 Kudos