ThinkGeo.com    |     Documentation    |     Premium Support

Close shapefile and delete

Hi, I want to display a shapefile and then edit or delete it, but I get the error: "The process cannot access the file 'C:\Zone.shp' because it is being used by another process."   How must I close the shapefile to release it? I am using this code:



 



Dim newPolyLineShapeFile As New ShapeFileFeatureLayer("Zone.shp", ShapeFileReadWriteMode.ReadWrite) 



newPolyLineShapeFile.Open()


newPolyLineShapeFile.FeatureSource.BeginTransaction()


newPolyLineShapeFile.FeatureSource.AddFeature(Feature)


newPolyLineShapeFile.FeatureSource.CommitTransaction()



winformsMap1.StaticOverlay.Layers.Add(newPolyLineShapeFile)



newPolyLineShapeFile.Close()



newPolyLineShapeFile =


Nothing

 



File.Delete(


"Zone.shp")

 



 


 



Alta, 
  
   You are doing the right things.  The .Close should release all of the locks on the files involved.  We will look right into it.  The idea behind the .Open and .Close is to handle this.  We use it to know if the files are ready for serialization and that everything gets shut down properly.  We should have an answer for you tomorrow. 
  
   I will also look to see if you need to rebuild the index file.  I don’t think so but I will check just to make sure.  Thanks for reporting this! 
  
   On other small thing is that if you post lots of code you can make it look better by wrapping the code in a code block.  If you are interested check out the post below.  Just my one pet peeve. :-) 
  
 gis.thinkgeo.com/Support/DiscussionForums/tabid/143/aff/21/aft/4941/afv/topic/Default.aspx 
  
 David

Alta, 


I couldn’t recreate your problem and the file is removed successfully on my machine.   Here as following is all my code please have a look. I guess you are not using the latest version (3.0.131) so I make the version number prompt out to make you easier to check.
 
 Code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show(ThinkGeo.MapSuite.DesktopEdition.WinformsMap.GetVersion(), "Version Number")
        Dim Feature As New Feature("Point(3 3)")

        Dim shapeFilePath = "MajorCities.shp"

        Dim newPolyLineShapeFile As New ShapeFileFeatureLayer(shapeFilePath, ShapeFileReadWriteMode.ReadWrite)
        newPolyLineShapeFile.Open()
        newPolyLineShapeFile.FeatureSource.BeginTransaction()
        newPolyLineShapeFile.FeatureSource.AddFeature(Feature)
        newPolyLineShapeFile.FeatureSource.CommitTransaction()
        winformsMap1.StaticOverlay.Layers.Add(newPolyLineShapeFile)
        newPolyLineShapeFile.Close()

        File.Delete(shapeFilePath)
End Sub

My Version Number Prompt



BTW, we will have a new version released around 22rd this month with many cool features added. Please try the new one then. :)


Ben.

Hi Sorry about the way I posted the code - was lazy.

I am using Mapsuite 3.0.131, Map Suite Desktop Full Edition 3.0 Setup.msi, Microsoft Visual Basic 2005, XP professional, sp2

and the example in gis.thinkgeo.com/Support/Dis...fault.aspx

I still get the error - please see the attachment.



298-Err_del_file.docx (156 KB)

I think the difference in the code is that I displayed the layer -  must clear the layer before I delete the file

Alta, 
  
 In the method winformsMap1.Refresh(), we opened all the including layers and don’t close them for a performance reason, so the layers are all locked after calling Refresh(). To remove a file, you need to add one line of code to close that layer first and then delete the date file. 
  
 Thanks, 
  
 Ben