In C#, how do I get the path of a layer file? I can get the datasource and name of the layer but I need to get the path to the .lyr file. I want to record the path so my extension can copy and load the .lyr file later.

This gives the path to the datasource (.mdb geodatabase in my case) that the .lyr file references:

var dataset = (IDataset) map.Layer[0];
var name = dataset.Workspace.PathName + "\\" + dataset.Name

Using that info plus the FeatureClass name I can open the layer and get the layer's polygons on the map but it is not the same as opening the .lyr file.

1 Answer 1


Once the layer is added to the map, the location of the .lyr file is not retained with the layer. There is no linkage to the original file. You could use a layer extension to store the path in custom code to store the path for use later.

  • Thanks. At least now I know it is not possible to do the way I was thinking. Is the location of the .lyr file retained somewhere else? I feel it must be possible to get because reopening a saved mxd loads the .lyr file so is the path stored in any object somewhere else? I don't know if the layer extension will work because I don't have control over which .lyr files are input. My tool uses an mxd that was already created. Commented Feb 4, 2012 at 18:57
  • 1
    The entire layer gets saved in the MXD. The .lyr file is not referenced after it is added. If you are adding the layers via a custom tool, you could store a property set in the layer extension with any information about the layers origin to retrieve later. Commented Feb 5, 2012 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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