This topic has been discussed before on the ESRI forum, see below.
Export lyr to relative path
The issue that you can't go higher in the file hierarchy is really
annoying.
But there is a difference when you save the layer via ArcMap:
Make sure that "Store relative path names to data sources" is active.
Rightclick a layer in the TOC, choose "Save as Layer File...") and
store the .lyr on your disk.
The Layer is stored in a binary lyr-File. Open the lyr-File with a
simple texteditor (e.g. Windows-Notepad).
You can find out the path to the data source stored in the lyr file.
For Example: . . \ . . \ . . \ . . \ d a t a b a s e \ T e s t . g d
b
That means that relative from the lyr-File the data source is four
folders higher then folder database and there Test.gdb.
If you save the lyr-File with ArcObjects the following path is stored:
d a t a b a s e \ T e s t . g d b
It's the incorrect path to datasource.
Why can't I save the lyr-File with the correct relative path to
datasource?
I used the following code (C#) to save the lyr-file:
private void saveLyr(string lyrFileName, IFeatureLayer pFLayer)
{
try
{
if (String.IsNullOrEmpty(lyrFileName)) return;
if (pFLayer == null) return;
//Layer ermitteln
IDataLayer2 pDataLayer = pFLayer as IDataLayer2;
IName pname = pDataLayer.DataSourceName;
IDatasetName2 datasetName = pname as IDatasetName2;
IWorkspaceName workspaceName = datasetName.WorkspaceName;
string pathToDB = Path.GetDirectoryName(workspaceName.PathName);
pDataLayer.RelativeBase = pathToDB;
saveToLayerFile(lyrFileName, pDataLayer as ILayer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void saveToLayerFile(string layerFilePath, ILayer layer)
{
try
{
if (layer == null) return;
//create a new LayerFile instance
ILayerFile layerFile = new LayerFileClass();
//make sure that the layer file name is valid
if (Path.GetExtension(layerFilePath) != ".lyr")
return;
if (layerFile.get_IsPresent(layerFilePath))
File.Delete(layerFilePath);
//create a new layer file
layerFile.New(layerFilePath);
//attach the layer file with the actual layer
layerFile.ReplaceContents(layer);
//save the layer file
layerFile.Save();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}