//Now Loop through LYR Files if there are any and Process them.. for (int y = 0; y < LYList.Count; y++) { string test = LayerFileLocation + "\\" + LYList.ToString(); ILayerFile l = new LayerFileClass(); l.Open(LayerFileLocation + "\\" + LYList .ToString()); //I'm not sure what to do with a Layer file from here. With an MXD I used the below code to loop through each layer and process it using IMapAdmin2.FireChangeFeatureclass() }
UID pUID = new UIDClass(); pDoc.Open(MXDLocation + "\\" + AL.ToString());//+ fi.Name); for (int i = 0; i < pDoc.MapCount; i++) { pMap = pDoc.get_Map(i); UID uid = new UIDClass(); //uid.Value = "{EDAD6644-1810-11D1-86AE-0000F8751720}"; // IGroupLayer uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; //IFeatureLayer IEnumLayer layers = pMap.get_Layers(uid, true); //Returns all layers including layers in grouplayers layers.Reset(); pFeatureLayer = layers.Next() as IFeatureLayer; while (pFeatureLayer != null) {...Etc..etc
Solved! Go to Solution.
Greetings,
I'm building an Add-in that lets users change datasources of all MXD's and Layer files in a directory of their choice. I have the MXD part working but I cannot figure out the Layer file part. In my code below, I've gathered a list of Layer files in this directory but now I need to open each layer file and change the data source information. Can anyone shed some light on this? Many thanks//Now Loop through LYR Files if there are any and Process them.. for (int y = 0; y < LYList.Count; y++) { string test = LayerFileLocation + "\\" + LYList.ToString(); ILayerFile l = new LayerFileClass(); l.Open(LayerFileLocation + "\\" + LYList .ToString()); //I'm not sure what to do with a Layer file from here. With an MXD I used the below code to loop through each layer and process it using IMapAdmin2.FireChangeFeatureclass() }
Here is what I did for MXD's..UID pUID = new UIDClass(); pDoc.Open(MXDLocation + "\\" + AL.ToString());//+ fi.Name); for (int i = 0; i < pDoc.MapCount; i++) { pMap = pDoc.get_Map(i); UID uid = new UIDClass(); //uid.Value = "{EDAD6644-1810-11D1-86AE-0000F8751720}"; // IGroupLayer uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; //IFeatureLayer IEnumLayer layers = pMap.get_Layers(uid, true); //Returns all layers including layers in grouplayers layers.Reset(); pFeatureLayer = layers.Next() as IFeatureLayer; while (pFeatureLayer != null) {...Etc..etc
Did you already try ILayerFile.ReplaceContents?
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00490000004v000000
With a layer file, you open it using ILayerFile and get the layer reference from the Layer property. Only one layer can be stored in a layer file. However, this layer may be a group layer so you'll need to handle that possibility.
With a layer file, you open it using ILayerFile and get the layer reference from the Layer property. Only one layer can be stored in a layer file. However, this layer may be a group layer so you'll need to handle that possibility.
You check the type of the layer. In C# that's the Is operator.
If (layer Is IGroupLayer) then...
You can access the layers within a group layer using the ICompositeLayer interface. For your code to be truly robust, you'll need to handle the possibility that group layers can contain other group layers so you can't just simply loop through the layers. You'll need to write a routine that loops through the layers within the group layer and updates the datasources of the feature layers and calls itself recursively for the group layers. As for changing the datasource, that's done the same way you did it for the mxd.