Delete a file being used by another process
In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoad
flag. One way to do this would be this:
string filename = ...
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filename);
image.EndInit();
Although setting BitmapCacheOption.OnLoad
works on a BitmapImage that is loaded from a local file Uri, this is afaik nowhere documented. Therefore a probably better or safer way is to load the image from a FileStream, by setting the StreamSource
property instead of UriSource
:
string filename = ...
BitmapImage image = new BitmapImage();
using (var stream = File.OpenRead(filename))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
How to delete file and that file is used by another process using C#
Replacing the image sounds like a good idea - but don't forget to dispose of the old Image
that's still holding the file open (and will, by default, until that Image
is garbage collected - at some unknown time in the future):
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
var old = myPictureBox.Image;
myPictureBox.Image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + @"\Images\defaultImage.jpg");
old.Dispose();
System.IO.File.Delete(txtFileName.Text);
MessageBox.Show("File Delete Sucessfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
(It may also be possible to Dispose
of the Image
directly without replacing the image for the PictureBox
- it depends on what else you're going to do after deletion - e.g. if the form on which the PictureBox
appears is closing that you may want to let that happen first and then just directly dispose of the image).
File.Delete cannot delete a file because it is being used by another process. Not sure how to fix this
I would say it is likely that GnuPG is still using the file when you try to delete it.
You need to wait for the process to end before doing the deletion, i.e. add:
proc.WaitForExit();
directly before
System.IO.File.Delete(fileName);
How To Delete A File Being Used By Another Process?
This code below ended up working for me:
public void OpenGeodatabase()
{
Geodatabase gdb = null;
// path to .geodatabase
var gdbPath = @"..\..\..\test.geodatabase";
// wrap OpenAsync call in Task
Task.Run(async () =>
{
// open a geodatabase on the local device
gdb = await Geodatabase.OpenAsync(gdbPath);
}).Wait();
// get the first geodatabase feature table
var gdbFeatureTable = gdb.FeatureTables.FirstOrDefault();
// create a layer for the feature table
var lyr = new FeatureLayer
{
ID = gdbFeatureTable.Name,
DisplayName = gdbFeatureTable.Name,
FeatureTable = gdbFeatureTable
};
// add the graphics to the map
MyMapView.Map.Layers.Add(lyr);
// remove the layer - to make it similar to case explanation
MyMapView.Map.Layers.Remove(lyr);
// make gdb reference null
gdb = null;
gdbFeatureTable = null;
lyr = null;
// call garbage collector
GC.Collect();
GC.WaitForPendingFinalizers();
// If the works, the lock has been removed
System.IO.File.Delete(@"..\..\..\test.geodatabase");
}
Basically I did everything inside of a non-async method, wrapped the call to OpenAsync within a Task, then set everything made from the geodatabase to null after use. Finally, I called the garbage collector and deleted the file.
Cannot delete file because it is being used by another process, ASP.NET Core MVC
try
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(fullImagePath);
}
catch(Exception e){
}
java Cannot delete file, being used by another process
I guess with sameSha1
you open SHA1.txt
to read it and you forget to close it.
EDIT:
From your comment you contain the following line in sameSha1
:
String sha1Txt = new Scanner(new File("SHA1.txt")).useDelimiter("\\Z").next();
So you create a scanner instance but you don't explicitly close it. You should do something like that:
Scanner s = new Scanner(new File("SHA1.txt"));
try {
String sha1Txt = s.useDelimiter("\\Z").next();
...
return result;
}
finally {
s.close();
}
Or as @HuStmpHrrr suggests in Java 7:
try(Scanner s = new Scanner(new File("SHA1.txt"))) {
String sha1Txt = s.useDelimiter("\\Z").next();
...
return result;
}
Related Topics
What Is the Purpose of the "Prefer 32-Bit" Setting in Visual Studio and How Does It Actually Work
Why Doesn't C# Infer My Generic Types
Cast Generic≪Derived≫ to Generic≪Base≫
How to Update the Current Line in a C# Windows Console App
Picturebox Paintevent With Other Method
What Is the { Get; Set; } Syntax in C#
Compare Two List≪T≫ Objects For Equality, Ignoring Order
C# Constructor Execution Order
How to Do Joins in Linq on Multiple Fields in Single Join
Why Is Access to the Path Denied
How to Easily Check If Access Is Denied For a File in .Net
Why Catch and Rethrow an Exception in C#
Dynamically Replace the Contents of a C# Method
Generating Random, Unique Values C#
Find Control by Name from Windows Forms Controls
Show Console in Windows Application
How to Modify a CSS Style in the Code Behind File for Divs in ASP.NET