打开、保存、另存mxd文件
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.全局变量
IMapDocument mapDoc;
2.在构造函数里面初始化全局变量
mapDoc = new MapDocumentClass();
3.打开mxd文件
Private void OpenMxd()
try
{
//打开文件对话框选取MXD文件
OpenFileDialog openFileDialog2;
openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "打开mxd文件";
openFileDialog2.Filter = "地图文档(*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
//将数据载入pMapDocument并与map控件联系起来
mapDoc.Open(sFilePath, "");
axMapControl1.Map.ClearLayers();
int i;
for (i = 0; i <= mapDoc.MapCount - 1; i++)
{
//一个IMapDocument对象中可能有多个Map对象,遍历每个map对象
axMapControl1.Map = mapDoc.get_Map(i);
}
//刷新地图
axMapControl1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
4.保存mxd文件
private void SaveMxd()
{
if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
{
MessageBox.Show("这个文件是只读的!");
return;
}
mapDoc.Save(mapDoc.UsesRelativePaths, true);
MessageBox.Show("保存成功!");
}
5.另存为mxd文件
private void SaveAsMxd()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "另存mxd文件";
saveFileDialog1.Filter = "地图文档(*.mxd)|*.mxd";
saveFileDialog1.ShowDialog();
string sFilePath = saveFileDialog1.FileName;
if (sFilePath == "")
{
return;
}
if (sFilePath == mapDoc.DocumentFilename)
{
SaveDocument();
}
else
{
mapDoc.SaveAs(sFilePath, true, true);
MessageBox.Show("另存成功!");
}
}