打开地图文件和shape文件代码加载Mxd文档
代码加载Mxd文档
用代码添加Mxd文档,用到AxMapControl.LoadMxFile(sFilePath),我们只要将Mxd文档的路径传给这个方法即可
/// <summary>
/// 打开地图文件
/// </summary>
/// <returns>文件的完整路径</returns>
public string OpenMxd()
{
string MxdPath = "";
OpenFileDialog OpenMXD = new OpenFileDialog();
OpenMXD.Title = "打开地图";
OpenMXD.InitialDirectory = "F:";
OpenMXD.Filter = "地图文件(*.mxd)|*.mxd";
if (OpenMXD.ShowDialog() == DialogResult.OK)
{
MxdPath = OpenMXD.FileName;
}
return MxdPath;
}
/// <summary>
/// 打开Shape文件,需要两个参数,shp文件所在目录,文件名
/// </summary>
/// <returns>数组,shp文件所在目录,文件名</returns>
public string[] OpenShapeFile()
{
string[] ShpFile = new string[2];
OpenFileDialog OpenShpFile = new OpenFileDialog();
OpenShpFile.Title = "打开Shape文件";
OpenShpFile.InitialDirectory = "F:";
OpenShpFile.Filter = "Shape文件(*.shp)|*.shp";
if (OpenShpFile.ShowDialog() == DialogResult.OK)
{
string ShapPath = OpenShpFile.FileName;
//利用\\将文件路径分为两部分
int Postion = ShapPath.LastIndexOf("\\");
string FilePath = ShapPath.Substring(0, Postion);
string ShpName = ShapPath.Substring(Postion+1);
ShpFile[0] = FilePath;
ShpFile[1] = ShpName;
}
return ShpFile;
}
private void btnOpen_Click(object sender, EventArgs e)
{
//OpenFileDialog OpenMXD = new OpenFileDialog();
//OpenMXD.Title = "打开地图";
//OpenMXD.InitialDirectory = "F:";
//OpenMXD.Filter = "地图文件(*.mxd)|*.mxd";
//if (OpenMXD.ShowDialog() == DialogResult.OK)
//{
// string MxdPath = OpenMXD.FileName;
// axMapControl1.LoadMxFile(MxdPath);
// //控件的LoadMXFIle是用来加载地图文档的。可以封装成函数OpenMxd()
//}
axMapControl1.LoadMxFile(OpenMxd());
}