2

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); 
DaBears
  • 247
  • 1
  • 13
  • 1
    Neil Clemmons offered some great insight on how to do this using ArcObjects. Here is the link in case anyone is interested. http://forums.arcgis.com/threads/53808-How-can-I-modify-a-Layer-File-s-data-source-using-ArcObjects?p=184671&posted=1#post184671 – DaBears Mar 27 '12 at 14:25

1 Answers1

0

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

dark fader
  • 1,072
  • 6
  • 8
  • Hi dark fader- I was hoping to do this in C# using ArcObjects but I guess I will just make a seperate Add-in using arcpy as you suggested. – DaBears Mar 26 '12 at 12:29
  • Unless you are using the 10.1 beta you cannot create add-ins using arcpy. Maybe you meant a script tool? – blah238 Mar 28 '12 at 00:22