1

I am having difficulty figuring out how to remove .sr lock files from shapefiles using ArcObjects and C# after removing the associated layer from the map. I notice that using the straight UI in ArcMap that removing a layer will remove the lock file, but I cannot get this to happen programmatically. I have tried everything that I can think of, including calling Marshal.FinalReleaseComObject on all variables. The following is some paired down test code that simply loads a layer into the map and then immediately removes it. This should remove the lock files, but it does not. Am I missing something?

// Loop through the selected shapefiles and load into the map
pGxObjects.Reset();

while ((pGxDataset = (IGxDataset)pGxObjects.Next()) != null)
{
    IFeatureLayer pFeatLayer = new FeatureLayer();
    pFeatLayer.FeatureClass = (IFeatureClass)pGxDataset.Dataset;
    pFeatLayer.Name = pFeatLayer.FeatureClass.AliasName;
    pMxDoc.FocusMap.AddLayer(pFeatLayer);

    // Do something with the layer

    pMxDoc.FocusMap.DeleteLayer(pFeatLayer);
    Marshal.FinalReleaseComObject(pFeatLayer);
    Marshal.FinalReleaseComObject(pGxDataset);
}

Marshal.FinalReleaseComObject(pGxObjects);
GC.Collect();
GC.WaitForPendingFinalizers();
3
  • Have you tried searching ESRI geonet on lock files, that would be best place to research this question? – Hornbydd Mar 17 at 19:51
  • Hornbydd, I have checked out Esri geonet, but haven't had any luck with a solution there. The closest thing that I found was a suggestion to release all COM objects, which I am doing. – David Lewis Mar 17 at 20:21
  • 1
    Orphan locks have been a long-term Arc Desktop "feature". Removing a layer from the Arc Desktop UI is not guaranteed to remove the associated lock(s). Furthermore, stopping all Arc Desktop instances will sometimes leave behind orphan locks(!) This article may help: support.esri.com/en/technical-article/000011812 – Stu Smith Mar 17 at 21:21
0

This is an issue I've never solved in a satisfactory way. The only way in ArcMap I can reliably remove locks is to collapse all Catalog folders and right click > refresh them AND to open a new map in ArcMap. However, in code there's an ugly work around.

Each shapefile is several files all with the same name and a different extension. So when using a shapefile you can first copy all the files, bar the .sr.lock file, to a new location or to the same location with a different name, then work with that shapefile.

ArcMap does not lock shapefile's files. That means if you want to delete or replace the shapefile you can delete or overwrite all the shapefile's files with the exception of the lock files. The lock files should be deleted when ArcMap closes.

So putting it together you can copy the shapefile, do whatever you need to it, and then replace the original, and then refresh the ArcMap layer.

2
  • Danielm, thank you for the response. Unfortunately, I don't believe this will be a solution for me, because I am potentially opening 1000+ shapefiles and ArcMap is retaining lock files on all of the shapefiles. Eventually this causes ArcMap to choke and it bombs out. I am trying to prevent this from happening. That is why I need to do away with the lock files. As an fyi, I am cycling through folders and temporarily loading in the shapefiles so that I can work with them and then I remove them from the map and create a new map document for the next folder. – David Lewis Mar 18 at 16:56
  • I guess my questions would be, do you need to load and unload into the table of contents or can you work with these shapefiles directly? Does this need to be done within ArcMap? In my experience lock files can be a little more cooperative if not loaded into the map. Also if this can be done outside of ArcMap, ending the thread the feature classes are opened in will release locks as well, so you can use multiple threads to accomplish your tasks. Additionally, if outside of ArcMap the number of locks may simply not be an issue. – danielm Mar 19 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.