0

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();
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 17, 2021 at 19:41
0

1 Answer 1

1

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.

answered Mar 18, 2021 at 16:19
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. Commented Mar 18, 2021 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. Commented Mar 19, 2021 at 15:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.