How to save a layer file (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with and configuring maps, layers, and graphics > Working with layers and renderers > How to save a layer file (ArcObjects .NET 10.6 SDK)

How to save a layer file


Summary
This topic shows how to save a layer in your map as a file on disk.

Saving a layer file

One of the main features of a layer is that it can exist outside your map as a file on disk. This makes it easy for others to access the layers you've built.
When you save a layer to disk, you save everything about the layer, such as the symbolization and labeling. When you add a layer file to another map, it will draw exactly as it was saved. Others can drop these layers onto their maps without having to know how to access the database or classify the data; this can be helpful when sharing data stored in a multiuser geodatabase with nontechnical staff members. You can share layers over a network, e-mail layers with the data, or enclose the layer in the data's metadata.
A complete sample can be found in Save a layer file in a MapControl application.
To save layer files programmatically in an ArcGIS Engine application, perform the following steps:
  1. Get the output path for the new layer file—You can use SaveFileDialog or any other form to get the output file name for the new layer file. The layer file must have a .lyr extension. See the following code example:
[C#]
SaveFileDialog saveFileDialog=new SaveFileDialog();
saveFileDialog.Filter="Layer File|*.lyr|All Files|*.*";
saveFileDialog.Title="Create Layer File";
saveFileDialog.RestoreDirectory=true;
saveFileDialog.FileName=System.IO.Path.Combine(saveFileDialog.InitialDirectory,
    layer.Name + ".lyr");
//Show the dialog box.
DialogResult dr=saveFileDialog.ShowDialog();
[VB.NET]
Dim saveFileDialog As SaveFileDialog=New SaveFileDialog()
saveFileDialog.Filter="Layer File|*.lyr|All Files|*.*"
saveFileDialog.Title="Create Layer File"
saveFileDialog.RestoreDirectory=True
saveFileDialog.FileName=System.IO.Path.Combine(saveFileDialog.InitialDirectory, layer.Name & ".lyr")
'Show the dialog box.
Dim dr As DialogResult=saveFileDialog.ShowDialog()
  1. Create a new instance of a LayerFile class—Use the LayerFile class to create a new instance of a layer file. See the following code example:
[C#]
//Create a new LayerFile instance.
ILayerFile layerFile=new LayerFileClass();
//Create a new layer file.
layerFile.New(saveFileDialog.FileName);
[VB.NET]
'Create a new LayerFile instance.
Dim layerFile As ILayerFile=New LayerFileClass()
'Create a new layer file.
layerFile.New(saveFileDialog.FileName)
  1. Bind the layer from the map with the newly created layer file—Once you have created the new layer file, you need to bind it with the current layer from the map that you intend to save to disk. See the following code example:
[C#]
//Bind the layer file with the layer from the map.
layerFile.ReplaceContents(layer);
[VB.NET]
'Bind the layer file with the layer from the map.
layerFile.ReplaceContents(layer)
  1. Save the layer file—To complete the operation, save the layer file. See the following code example:
[C#]
layerFile.Save();
[VB.NET]
layerFile.Save()


See Also:

Samples:




Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced