Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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. –  thelastlombax Feb 4 '12 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. –  Craig Williams Feb 5 '12 at 8:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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