[GIS] How to modify a LYR file using ArcObjects

arcobjectsc

I'm creating an Add-in for ArcCatalog using C# that modifies the datasources of layers in a MXD and .LYR files by themselves. I have gotten the MXD code to work but I'm lost with how to modify a .LYR file without the use of IMap. Does anyone have any code suggestions?

To load an MXD and loop and start modifying layers in the TOC was simply something like:

UID pUID = new UIDClass();

            pDoc.Open("C:\\Test\\File1.mxd");

            for (int i = 0; i < pDoc.MapCount; i++)
            {
                pMap = pDoc.get_Map(i);

                UID uid = new UIDClass();
                uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; //IFeatureLayer
                IEnumLayer layers = pMap.get_Layers(uid, true);       //Returns all layers

But I'm unsure how to get this process going for standalone .LYR files..

ILayerFile l = new LayerFileClass();
l.Open(fileName); 

Best Answer

Do you just need to do "l.Layer"? Hard to say since not sure what\how you do the processing after "IEnumLayer layers = pMap.get_Layers(uid, true);".

One point to note is the .lyr file's Layer could be a group layer so you would need to check and gets its contents (and ensure its not a grouplayer in grouplayer ...), basically some sort of recursive function.

Have said the above, depending upon what version you are on you might look at arcpy.

gis.stackexchange ....change-data-source-path-in-lyr-files-in-arcgis-10

Related Question