In using arcpy, I have found that there are just a few lines of code needed to get a list of layers in a LYR file if more than one layer exist. How can I accomplish this same feat using ArcObjects in C#? I know that ILayerFile is the way to start this off and I can open a specific layer file using ILayerFile.Open() but how can I get back a list of layers in this layer file if more than one exist?

  • 1
    I'm sure I have an extension method stuffed somewhere that gets this, but I'm not at my computer. I think you need to QI ICompositeLayer==>ILayerFile.Layer to get your layers: forums.esri.com/Thread.asp?c=93&f=993&t=130402#770246 (VB Example) – Jay Cummins Mar 27 '12 at 22:08
  • Just saw your other question (where you posted a comment that links to a Neil Clemmons post that pretty much answers your question). Is that not working? – Jay Cummins Mar 27 '12 at 22:16
  • Hi Jay- Your answer was correct and I would like to give you the credit since you answered first. Can you add your answer to the actual answer block below instead of the comments. Blah238 was right too and I'll give him some points but he posted after you – DaBears Mar 28 '12 at 12:07
  • Don't worry about it! He took the time to write a good, readable answer. – Jay Cummins Mar 28 '12 at 18:02

Query interface from ILayerFile.Layer to ICompositeLayer to determine whether the layer is a group/composite layer.

If the QI succeded, check ICompositeLayer.Count to see if it has any sublayers.


ILayerFile layerFile = new LayerFileClass();
ICompositeLayer compositeLayer = layerFile.Layer as ICompositeLayer;
if (compositeLayer != null && compositeLayer.Count > 0)
    // do something with the sublayers

You can then access a sublayer by its index (probably using a for loop) using the ICompositeLayer.Layer property, which, because it takes an argument should be called as a method in .NET, e.g.: compositeLayer.get_Layer(i).

If you think the layer file might have multiple nested group layers, you would want to write a recursive function to loop through each sublayer and recurse if it is also a composite layer or do something else with it if not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.