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();