内容发布更新时间 : 2024/11/19 0:44:18星期一 下面是文章的全部内容请认真阅读。
private void button8_Click(object sender, EventArgs e) {
IWorkspace pWs = GetWorkspace(@\
IMapDocument pDoc= new MapDocumentClass(); pDoc.Open(axMapControl1.DocumentFilename); ChangePath(pDoc, pWs); } //替换路径
private void ChangePath(IMapDocument pMapDoc, IWorkspace pWs) {
IWorkspaceName pWsName = GetWorkspaceName(pWs); IMap pMap = pMapDoc.ActiveView.FocusMap; ILayer pLayer = null;
IDataLayer pDataLayer = null; IDatasetName pDataName = null; for (int i = 0; i < pMap.LayerCount; i++) {
pLayer = pMap.get_Layer(i); if (pLayer.Valid == false) {
if (pLayer is IDataLayer) {
pDataLayer = pLayer as IDataLayer; try {
pDataName = pDataLayer.DataSourceName as IDatasetName; //获取DatasetName,必须是要替换的工作空间下的
IDatasetName pDsName = GetDatasetName(pWs, pDataName.Name); pDataName = pDataLayer.DataSourceName as IDatasetName; pDataName.WorkspaceName = pWsName; pDataLayer.Connect(pDsName as IName);
pDataName = pDataLayer.DataSourceName as IDatasetName; } catch { } } } }
pMapDoc.Save(true, true);
}
private IDatasetName GetDatasetName(IWorkspace pWs, string pFtName) { try {
IEnumDatasetName pDsName = pWs.get_DatasetNames(esriDatasetType.esriDTAny); IDatasetName pDsSetName = pDsName.Next(); while (pDsSetName != null) {
//遍历FeatureDataset
if (pDsSetName.Type == esriDatasetType.esriDTFeatureDataset) {
IEnumDatasetName pDsFtName = pDsSetName.SubsetNames; IDatasetName pDsSetName1 = pDsFtName.Next(); while (pDsSetName1 != null) {
if (pDsSetName1.Name == pFtName) return pDsSetName1;
pDsSetName1 = pDsFtName.Next(); } }
else if (pDsSetName.Name == pFtName) return pDsSetName;
pDsSetName = pDsName.Next(); }
return pDsSetName; }
catch (Exception ex) { return null; } }
private IWorkspaceName GetWorkspaceName(IWorkspace pWs) { try {
IDataset pDs = pWs as IDataset; return pWsName; }
catch (Exception ex) { return null; } }
public IWorkspace GetWorkspace( String _pDatabase) {
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWsFactory = null; IWorkspace pWkspace = null;
if(System.IO.Path.GetExtension(_pDatabase).Equals(\ {
pWsFactory = new AccessWorkspaceFactoryClass(); }
else if(System.IO.Path.GetExtension(_pDatabase).Equals(\ {
pWsFactory =new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); } else {
pWsFactory = new ShapefileWorkspaceFactory(); } try {
pWkspace = pWsFactory.OpenFromFile(_pDatabase, 0); }
catch (Exception EX) {
//MessageBox.Show(EX.ToString()); }
return pWkspace; }