This document is archived and information here might be outdated.  Recommended version.

IMapControl2.AddLayerFromFile Method (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > ArcObjects namespaces > Controls > ESRI ArcGIS Controls > Interfaces > IM > IMapControl2 Interface > IMapControl2.AddLayerFromFile Method
ArcGIS Developer Help

IMapControl2.AddLayerFromFile Method

Loads a layer file and adds it to the Map's collection of layers at the specified index position.

[Visual Basic .NET]
Public Sub AddLayerFromFile ( _
    ByVal lyrPath As String, _
    [ByVal toIndex As Integer] _
public void AddLayerFromFile (
    string lyrPath,
    int toIndex
HRESULT AddLayerFromFile(
  BSTR lyrPath,
  long toIndex
lyrPath [in]

lyrPath is a parameter of type BSTR toIndex [in, optional, defaultvalue()]
toIndex is a parameter of type long

Product Availability

Available with ArcGIS Engine.


When a layer file is added to the MapControl's layer collection and no index is specified, it is added to the beginning or top of the collection with an index of 0. The numeric index of any other layers in the layer collection increments by one.

When a layer file is added and an index is specified, the numeric index of any other layers in the layer collection will increment by one, except for those layers with an index less than the specified index. The index of these layers will remain the same.

Errors Returned

1023 800a03ff: The specified index is out of range


The Layer at the beginning or top of the collection with an index of 0 will be drawn last, whilst the Layer at the bottom of the collection will be drawn first.

Use the MoveLayerTo method to change the position of a Layer within the layer collection.

//Add layer at index 0
//Add layer at index 2
axMapControl1.AddLayerFromFile(@"C:\Temp\myLayer.lyr", 2);
[Visual Basic .NET]
'Add layer at index 0
'Add layer at index 2
AxMapControl1.AddLayerFromFile("C:\Temp\myLayer.lyr", 2)

See Also

IMapControl2 Interface | IMapControl4.AddLayer Method | IMapControl2.AddLayer Method | IMapControl3.AddLayer Method | IMapControl3.AddShapeFile Method | IMapControl2.AddShapeFile Method | IMapControl4.AddShapeFile Method

.NET Samples

Geoevents in a MapControl