ArcMap addin locking newly created shp files

2424
2
11-15-2012 09:02 PM
MatejSkerjanc
Occasional Contributor
Hello
i hope this is the right subforum. I created arcmap addin in c# some data export into shape. When the tool is done and the shape files are ready for use i get the following error upon trying to pull them onto the ArcMap's table of content:

One or more layers failed to draw:

586537922002012111601_ILL:  Cannot acquire a lock.
586537922002012111601_ILL:  Cannot acquire a lock.
586537922002012111601_IPL:  Cannot acquire a lock.
586537922002012111601_IPL:  Cannot acquire a lock.
586537922002012111601_ITL:  Cannot acquire a lock.
586537922002012111601_ITL:  Cannot acquire a lock.

What could be locking my files?


this is where i create shape files
...
...

            //Get the factory
            ShapefileWorkspaceFactory factory = new ShapefileWorkspaceFactory();

            //Create a workspace
            IWorkspaceName workspaceName = factory.Create(IzvozHelper.wrkdir.Substring(0, 3), IzvozHelper.wrkdir.Substring(3, IzvozHelper.wrkdir.Length - 3), null, 0);




            IName name = (IName)workspaceName;

            //QI to IFeatureWorkspace
            IFeatureWorkspace featWorkspace = (IFeatureWorkspace)name.Open();

            //Create FeatureClass
            var filename = getFileName(int.Parse(IzvozHelper.tip)).Substring(1, getFileName(int.Parse(IzvozHelper.tip)).Length - 1);
            globalFileName = filename;
            var fullname = IzvozHelper.wrkdir + "\\" + filename;
            string[] fileList = System.IO.Directory.GetFiles(IzvozHelper.wrkdir, filename + ".*");


            try
            {
                foreach (string s in fileList)
                {
                    if (s.EndsWith("shp") || s.EndsWith("shx") || s.EndsWith("dbf") || s.EndsWith("prj"))
                        File.Delete(s);
                }
            }
            catch (IOException e)
            {
                MessageBox.Show("xxxxx!\r\n" + e.ToString());
            }

            //if (IzvozHelper.tip == "1")
            featWorkspace.CreateFeatureClass(filename, fields, null, null, esriFeatureType.esriFTSimple, shapeFieldName, "");

            return featWorkspace.OpenFeatureClass(filename);
}
0 Kudos
2 Replies
nicogis
MVP Frequent Contributor
try post on forum arcobjects: http://forums.arcgis.com/forums/20-ArcObjects-All-Development-Languages

However in code I suggest:

for delete a shapefile use IDataset :: Delete so you don't check ext files (you can have also sbn,sbx,ain,aix (index on field shape or field) ect)

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Delete_Method/0025000001...

and if you use singleton class instance it with activator
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/0001/00010000043p000000.htm



Type t = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
System.Object obj = Activator.CreateInstance(t);
...

0 Kudos
MatejSkerjanc
Occasional Contributor
Thanks for the reply, i kinda figured i needed to release com objects

Marshal.ReleaseComObject(ShapeHelper.featureCursorInsert);
Marshal.ReleaseComObject(ShapeHelper.featureBufferInsert);
this kinda fixed the issue. Thank you for the reply, i'll still read those links.


Matej
0 Kudos