Shapefile LOCK files

4267
11
02-03-2011 06:52 AM
MatthewMalinowski
New Contributor
Hello,

I have a WCF service that opens a collection of shapefiles and uploads the data from these shapefiles into their respective SDE feature classes.  Everything works perfectly except at the very end when I wish to delete the shapefiles as they are stored in a temporary location on the server. The act of opening a featurecursor on the shapefiles creates a LOCK file the same as if you were to view the file in ArcCatalog.  I do everything that I can possibly think of to release the lock on the shapefile, including System.Runtime.Interop.Marshal.ReleaseComObject & FinalReleaseComObject (neither works) on all ArcObjects that I use along with shutting down the ArcObjects license that I check out at the beginning of the whole procedure. I cannot find any hints as to how to release the lock on the shapefile.  It eventually goes away if I wait long enough, but I need for it to go away immediately after I am done looping through the feature cursor.  Does anyone have any ideas on how to release these locks? I tried to use the ESRI.ArcGIS.esriSystem.IAppLockMgr but I cannot find any examples on how to use this and I'm not even sure if it is the correct interface to use for this.

Any help would be GREATLY appreciated.

Thanks in advance,

- Matt
0 Kudos
11 Replies
GregoryRoss
New Contributor II
Is this an exclusive lock (.ex.lock) or a shared lock (.sr.lock)? I belive you can only release an exclusive lock. Also, I think shared locks are not release until the calling app is disposed....
0 Kudos
AlexanderGray
Regular Contributor II
Locks are caused by the interaction of ArcObjects COM and .NET framework.  You need to explicitly release any objects referring to the shapefile using the comreleaser.  This includes any cursor, feature, row, featureclass or workspace objects.  The locks will be released when the calling app is disposed because that will release all the objects.  If you explicitly release all the objects by calling comreleaser, you should be able to release the locks without terminating the process.
0 Kudos
DaveLewis3
New Contributor

I realize that this posting is 10 years old, so I might not get a response.  However, 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.

0 Kudos
KirkKuykendall1
Occasional Contributor III

The IMxDocument.OperationStack might still have a reference to the layer. 

Maybe try IOperationStack.Reset() to see if that clears the lock. You won't be able to Edit>Undo (ctrl-Z) to the layer removal after doing so.

0 Kudos
DaveLewis3
New Contributor

Kirk, thank you for the response.  I tried your suggestion and unfortunately it did not make a difference.  The only thing that seems to kill the .sr.lock files is manually shutting down the ArcMap application, which gets rid of all of the COM references.  However, this is not a solution, since I have so many directories worth of shapefiles to run through.  The following is a little snippet from my code to show the issue.

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

0 Kudos
KirkKuykendall1
Occasional Contributor III

Maybe before deleting the layer, try IDataLayer2.Disconnect() on it.

0 Kudos
DaveLewis3
New Contributor

That was a great suggestion.  I had forgotten about IDataLayer2.  However, it did not work.  The lock file is still there.  I have noticed that the lock file isn't actually created when the layer is added to the map, but two lines earlier when the feature class is defined on the feature layer from pGxDataset.Dataset (for whatever help that is).

0 Kudos
KirkKuykendall1
Occasional Contributor III

In general, I find it good practice to work with IName objects. 

Maybe try replacing this:

pFeatLayer.FeatureClass = (IFeatureClass)pGxDataset.Dataset;

with this:

pFeatLayer.FeatureClass = ((IName)pGxDataset.DatasetName).Open() as IFeatureClass;

 

0 Kudos
DaveLewis3
New Contributor

That was another good suggestion, but unfortunately I have the same result.  The .sr.lock file is created and I can't remove it.

0 Kudos