ArcGIS加载数据中常用的File文件方法总结

在介绍ArcGIS中各种数据的打开方法时,我们用到了许多对于File文件的操作,在此做一个常用用法的总结。例如,

介绍ArcGIS中各种数据的打开方法——mxd(地图文档)

以方法一为例:运用LoadMxFile方法的函数参数加载地图文档

private void loadMapAccDoc1()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Title = "打开地图文档";
    ofd.Filter = "map documents(*.mxd)|*.mxd";
    ofd.InitialDirectory = m_Path;
    //判断, 如果对话框结构不为OK, 退出函数体
    DialogResult DR = ofd.ShowDialog();
    if (DR != DialogResult.OK)
        return;
    string filePath = ofd.FileName;
    if (axMapControl1.CheckMxFile(filePath))
    {
        //设置axMapControl控制鼠标指针图标选项为沙漏光标
        axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerArrowHourglass;
        //三个参数(filePath——文件路径、0——地址名称或索引、Type.Missing——通过反射进行调用获取参数的默认值)
        axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
        //定义axMapControl控制鼠标指针图标为默认箭头
        axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;
        axMapControl1.Extent = axMapControl1.FullExtent;
    }
    else
    {
        MessageBox.Show(filePath + "不是有效的地图文档");
    }
}

从以上的代码中,我们归纳出几条常用的。

1、

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开地图文档";
ofd.Filter = "map documents(*.mxd)|*.mxd";
ofd.InitialDirectory = m_Path;

2、

DialogResult DR = ofd.ShowDialog();
if (DR != DialogResult.OK)
    return;

可改写为(推荐使用)

if(ofd.ShowDialog()!=DialogResult.OK)
    return;

3、

因为C#当中的FileName得到完整的路径(路径+文件名),让人感到有点不理解,很容易会让人误以为只是得到文件名。

因此,我们在编码中规定,

pFileName(完整的路径,路径+文件名)

fileName(文件名)

filePath(路径)

因此方法一中,应改写为:

private void loadMapAccDoc1()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Title = "打开地图文档";
    ofd.Filter = "map documents(*.mxd)|*.mxd";
    ofd.InitialDirectory = m_Path;
    //判断, 如果对话框结构不为OK, 退出函数体
    if (ofd.ShowDialog() != DialogResult.OK)
        return;
    string pFileName = ofd.FileName;
    if (axMapControl1.CheckMxFile(pFileName))
    {
        //设置axMapControl控制鼠标指针图标选项为沙漏光标
        axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerArrowHourglass;
        //三个参数(pFileName——文件路径、0——地址名称或索引、Type.Missing——通过反射进行调用获取参数的默认值)
        axMapControl1.LoadMxFile(pFileName, 0, Type.Missing);
        //定义axMapControl控制鼠标指针图标为默认箭头
        axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;
        axMapControl1.Extent = axMapControl1.FullExtent;
    }
    else
    {
        MessageBox.Show(pFileName+ "不是有效的地图文档");
    }
}

4、

//FileInfo类提供创建、复制、删除、移动和打开文件的实例方法
FileInfo fileInfo = new FileInfo(ofd.FileName);
//获取父目录并强制转换成字符型
String filePath = fileInfo.Directory.ToString();
//得到不带后缀的文件名
String sfileName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf("."));

5、

string pFileName = ofd.FileName;
int index = 0;
//获取最后一个“\”时的索引位置
index = pFileName.LastIndexOf("\");
//获得shp文件的路径
string filePath = pFileName.Substring(0, index);
//获得shp文件名
string fileName = pFileName.Substring(index + 1, pFileName.Length - (index + 1));

谢谢观看!本人初学GIS二次开发,如果有不对的地方,请多多包涵!

原文地址:https://www.cnblogs.com/edcoder/p/11722563.html