导出shp文件出错,异常HRESULT:0x80040351
原因是后缀名重复。之前没有标记的那句。保存后的文件后缀名出现 ***.shp.shp
删除多余后缀名即可。
代码如下
private void 导出数据ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog m_save = new SaveFileDialog();
m_save.Filter = "SHP(*.shp)|*.shp|tiff(*.tif)|*.tif";
DialogResult diaR = m_save.ShowDialog();
if (diaR == DialogResult.OK)
{
string Outpath = m_save.FileName;
if (m_save.FileName.EndsWith(".tif"))
{
ILayer rLayer2 = LayerMenu.Tag as ILayer;
IRaster resultDataset = (rLayer2 as IRasterLayer).Raster as IRaster;
IWorkspaceFactory pWKSF = new RasterWorkspaceFactory();
ISaveAs pSaveAs = resultDataset as ISaveAs;
pSaveAs.SaveAs(Outpath, null, "TIFF");//以tif格式保存
}
if (m_save.FileName.EndsWith(".shp")) //导出为shp文件
{
Outpath = Outpath.Remove(Outpath.LastIndexOf("."));//删除多余后缀名
ILayer iLayer = LayerMenu.Tag as ILayer;
IFeatureLayer pFeatureLayer = iLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
string sFileName = System.IO.Path.GetFileName(Outpath);
string sFilePath = System.IO.Path.GetDirectoryName(Outpath);
IDataset pDataset = pFeatureClass as IDataset;
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(sFilePath, 0) as IFeatureWorkspace;
IWorkspace pWorkspace = pFeatureWorkspace as IWorkspace;
pDataset.Copy(sFileName, pFeatureWorkspace as IWorkspace);
}
}
}