Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using ESRI. ArcGIS. ADF;
Using ESRI. ArcGIS. Controls;
Using ESRI. ArcGIS. SystemUI;
Using ESRI. ArcGIS. esriSystem;
Using ESRI. ArcGIS. Geometry;
Using ESRI. ArcGIS. Carto;
Using ESRI. ArcGIS. Display;
1. Global Variables
IMapDocument mapDoc;
2. initialize global variables in the constructor
MapDoc = new MapDocumentClass ();
3. Open the mxd File
Private void OpenMxd (){
Try
{
// Select the MXD file in the open file dialog box
OpenFileDialog openFileDialog2;
OpenFileDialog2 = new OpenFileDialog ();
OpenFileDialog2.Title = "Open mxd file ";
OpenFileDialog2.Filter = "map document (*. mxd) | *. mxd ";
OpenFileDialog2.ShowDialog ();
String sFilePath = openFileDialog2.FileName;
// Load the data into pMapDocument and associate it with the map control.
MapDoc. Open (sFilePath ,"");
AxMapControl1.Map. ClearLayers ();
Int I;
For (I = 0; I <= mapDoc. MapCount-1; I ++)
{
// An IMapDocument object can have multiple Map objects and traverse each map object
AxMapControl1.Map = mapDoc. get_Map (I );
}
// Refresh the map
AxMapControl1.Refresh ();
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
}
4. Save the mxd File
Private void SaveMxd ()
{
If (mapDoc. get_IsReadOnly (mapDoc. DocumentFilename) = true)
{
MessageBox. Show ("this file is read-only! ");
Return;
}
MapDoc. Save (mapDoc. UsesRelativePaths, true );
MessageBox. Show ("saved successfully! ");
}
5. Save as an mxd File
Private void SaveAsMxd ()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog ();
SaveFileDialog1.Title = "Saving mxd Files ";
SaveFileDialog1.Filter = "map document (*. mxd) | *. mxd ";
SaveFileDialog1.ShowDialog ();
String sFilePath = saveFileDialog1.FileName;
If (sFilePath = "")
{
Return;
}
If (sFilePath = mapDoc. DocumentFilename)
{
SaveDocument ();
}
Else
{
MapDoc. SaveAs (sFilePath, true, true );
MessageBox. Show ("saved successfully! ");
}
}