AO解锁shp文件
AO开发中加载shp文件后,无论删除对象还是垃圾回收都不能解除shp文件的锁定,导致在其他地方无法对该图层进行操作,只有关闭应用程序才能删除加锁文件,彻底释放资源。
其实,AO中有专门的代码解除资源锁定,解锁文件:
1、存在接口IWorkspaceFactory的情况
IWorkspaceFactory pwf = new ShapefileWorkspaceFactory();
//关闭资源锁定
IWorkspaceFactoryLockControl ipWsFactoryLock = (IWorkspaceFactoryLockControl)pwf;
if (ipWsFactoryLock.SchemaLockingEnabled)
{
ipWsFactoryLock.DisableSchemaLocking();
}
2、仅已知接口IFeatureClass的情况
/// <summary> /// 解锁IFeatureClass锁定的资源 /// </summary> /// <param name="pFeatureClass">锁定资源的要素类</param> /// <param name="releaseComObject">false-默认,不释放要素类;true-解锁并释放要素类</param> public static void UnlockResource(IFeatureClass pFeatureClass, bool releaseComObject = false) { if (pFeatureClass == null) return; IWorkspace pWorkspace = ((IDataset)pFeatureClass).Workspace; if (pWorkspace == null) return; //关闭资源锁定 try { IWorkspaceFactoryLockControl ipWsFactoryLock = (IWorkspaceFactoryLockControl)pWorkspace.WorkspaceFactory; if (ipWsFactoryLock != null && ipWsFactoryLock.SchemaLockingEnabled) ipWsFactoryLock.DisableSchemaLocking(); if (releaseComObject) ComReleaser.ReleaseCOMObject(pFeatureClass); } catch (Exception ex) { } }