图层数据源修改方法

当图层可以正常访问时,可以通过如下方式得到图层的数据源路径

                ILayer pLayer = axMapControl1.get_Layer(6);

                IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
                IFeatureClass fc = pFeatureLayer.FeatureClass;
                IDataset fds = fc as IDataset;
                IWorkspace ws = fds.Workspace;

                string s = ws.PathName.ToUpper( );

当图层不能正常访问,需要修复数据源时,上面方法获取的IFeatureClass为空,这时可以通过如下方式得到图层的数据源路径
IDataLayer pDLayer = (IDataLayer)pLayer;
IWorkspaceName ws = ((IDatasetName)(pDLayer.DataSourceName)).WorkspaceName;
this.textBox2.Text = ws.PathName;


数据源修改方法

1、fileGDB
IDataLayer2 pDLayer = (IDataLayer2)pFLayer;
IDatasetName pDsName = (IDatasetName)(pDLayer.DataSourceName);
IWorkspaceName ws = pDsName.WorkspaceName;
this.textBox2.Text = ws.PathName;
string newPath = @"D:\bin\nw.gdb";
ws.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
ws.PathName = newPath;
pDsName.WorkspaceName = ws;
pMapDocument.Save(true,true);

2、shp格式文件
IDataLayer2 pDLayer = (IDataLayer2)pFLayer;
IDatasetName pDsName = (IDatasetName)(pDLayer.DataSourceName);
IWorkspaceName ws = pDsName.WorkspaceName;
this.textBox2.Text = ws.PathName;
string newPath = @"D:\bin\";
ws.PathName = newPath;
pDsName.WorkspaceName = ws;
pMapDocument.Save(true,true);

你可能感兴趣的:(数据源)