Before I begin, I should mention that this isn't really something I want to do, I'm simply curious about how it works.
I have this method called AddLayer(), which opens a local geodatabase file and creates a map with it:
public async void AddLayer() { try { // open a geodatabase on the local device gdb = await Geodatabase.OpenAsync(@"..\..\..\test.geodatabase"); // get the first geodatabase feature table var gdbFeatureTable = gdb.FeatureTables.FirstOrDefault(); //create a layer for the feature table lyr = new FeatureLayer { ID = gdbFeatureTable.Name, DisplayName = gdbFeatureTable.Name, FeatureTable = gdbFeatureTable }; // add the graphics to the map MyMapView.Map.Layers.Add(lyr); } catch (Exception ex) { MessageBox.Show("Unable to create offline database: " + ex.Message); } return; } I also have another method called RemoveLayer(), which simply un-does everything AddLayer() did and then calls GC.Collect().
My question is, why, even after no longer using the resources in the file and calling the garbage collector, can I not delete the file (the geodatabase file) while the program is running?
Is this normal behavior for all programs in Windows? Is it to avoid somehow corrupting the program?
Thank you all for helping me understand this.
async void.gdbis a member variable, and you don't show where/host it is disposed. That being the result of an open operation is probably where any open file handles would be; but without knowing much more about it or how you're disposing it, it's hard to say what specifically you could do.