map为地图控件,layerName是图层名字
ILayer pLayer = MapControlHelper.GetLayerFromName(map, layerName);
if (pLayer != null && pLayer is IFeatureLayer)
{
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Title = "输出shp文件";
sfd.Filter = "矢量数据(*.shp)|*.shp";
if (sfd.ShowDialog() == DialogResult.OK)
{
Geoprocessor GP = new Geoprocessor();
GP.OverwriteOutput = true;
FeatureClassToFeatureClass exportSHP = new FeatureClassToFeatureClass();
//获得图层路径,其它文章中有其方法
string layerpath = MapControlHelper.getLayerPath(pLayer);
string outpath = sfd.FileName;
int index = outpath.LastIndexOf("\\");
//获得文件路径
string filePath = outpath.Substring(0, index);
//获得文件名
string fileName = outpath.Substring(index + 1);
exportSHP.in_features = layerpath;
exportSHP.out_path = filePath;
exportSHP.out_name = fileName;
try
{
GP.Execute(exportSHP, null);
MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
string str = "";
for (int i = 0; i < GP.MessageCount; i++)
{
str += GP.GetMessage(i);
str += "\n";
}
MessageBox.Show(str);
}
}
}
}