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).
C# File.Delete, file being used by another process
your process is the one that uses file , you need to set image to null
use something like this :
using(var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()))
pctrbFoto.Image = img;
Deleting files gives an error (The process cannot access the file..)
mail.dispose();
You should dispose mail before deleting the file. This should remove the lock on the file.
foreach (var filename in filenames)
{
var file = Path.Combine(filePath, filename);
mail.Attachments.Add(new Attachment(file));
}
// Send Mail
smtpServer.Send(mail);
mail.Dispose();
DeleteFiles();
https://msdn.microsoft.com/en-us/library/0w54a951(v=vs.110).aspx
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);
Related Topics
How to Check for a Valid Base64 Encoded String
Launch an Application and Send It to Second Monitor
How to Use Telegram API in C# to Send a Message
Replace Single Backslash With Double Backslash
Windows Service Cannot Access Network Location (Unc) Path
Convert Time With Milliseconds to Datetime in C#
Reading Only Specific Columns from a CSV File Out of Many
C#: How to Force Trailing Zero in Numeric Format String
Finding Closest Value in an Array
How to Make a Soap/Wsdl Client in C#
How to Get Number of Rows Using Sqldatareader in C#
First Call to Web API Is Very Slow C#
Deserialize Json Array to C# List Object
Asp.Net Core Get Json Array Using Iconfiguration
How to Check If Multiple Values in an Array Are Equal to One String