0

I'm using WinForms. I made a simple Image Viewer application using a picturebox to display my images. I made a way to create temporary files. These files are always picture files. When my application is done using the image i want to be able to delete these temporary on FormClosing files located at: C:\Users\taji01\AppData\Local\Temp\8bd93a0dec76473bb82a12488fd350af To do that i cannot simply call File.Delete(C://picture.jpg) because my application is still using them even though there is another picture displaying in my application. So i tried to dispose it but i couldn't figure how how to do that. Should i be using a using statement? Is there a better way to dispose and delete the file or is there a way to make this work?

  _fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
  File.Copy(imagePath, _fileName);
  _stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileOptions.DeleteOnClose);
  this._Source = Image.FromStream(_stream);

Error: "The process cannot access the file C:\picture.jpg because it is being used by another process" Exeption thrown: 'System.IO.IO.Exception' in msconrlib.dll (The process cannot access the file 'C:\picture.jpg' because it is being used by another procesas")

taji01
  • 2,527
  • 8
  • 36
  • 80
  • `Path.GetTempFileName()` – SLaks Nov 13 '16 at 01:33
  • https://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx#Examples – Jim Nov 13 '16 at 01:41
  • From the [docs for `Image.FromFile()`](https://msdn.microsoft.com/en-us/library/stf701f5(v=vs.110).aspx): *The file remains locked until the Image is disposed.* So you have to dispose the image first. Or make a copy in memory and dispose the original. See http://stackoverflow.com/questions/6576341/open-image-from-file-then-release-lock – dbc Nov 13 '16 at 01:48
  • And from the docs for [`Image.FromStream()`](https://msdn.microsoft.com/en-us/library/93z9ee4x(v=vs.110).aspx): *You must keep the stream open for the lifetime of the Image.* – dbc Nov 13 '16 at 01:52
  • Take a look at [this post](http://stackoverflow.com/a/38830222/3110834) – Reza Aghaei Nov 13 '16 at 07:39
  • Hey @RezaAghaei! ill take a look at that post. You should visit this website www.codementor.io and become a mentor since you help a lot. I go there often to get help from experienced mentors. The mentors get paid for helping. Check it out :) – taji01 Nov 13 '16 at 07:49
  • 1
    There is more than one problem here. But do pay attention to the exception message, it is *not* talking about the temporary file. C:\picture.jpg can only be the *imagePath*, not *_fileName*. So you are doing something else wrong with *magePath* that we can't see. Just fix this aggressively, forget about the temp files because they didn't solve anything. Before you assign `_Source` you probably don't need the old one anymore, so dispose it. Or if you can't figure it out then avoid all locking by using the Bitmap(Image) constructor to make an in-memory copy so you can dispose immediately. – Hans Passant Nov 13 '16 at 16:27

3 Answers3

0

You need to Close() your FileStream.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • do i close it in formclosing or when in the method im using it? – taji01 Nov 13 '16 at 01:34
  • 1
    You should close things as soon as you no longer need them. – SLaks Nov 13 '16 at 01:36
  • 1
    [`Image.FromStream()`](https://msdn.microsoft.com/en-us/library/93z9ee4x(v=vs.110).aspx) requires the stream to be kept open for the lifetime of the image. If you dispose the file stream manually it will cause problems down the road, for instance it will be impossible to save the image. See http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream – dbc Nov 13 '16 at 01:50
0

I think a Transaction manager will do what you want. Check out .NET Transactional File Manager. When you rollback your Transaction, it should delete your temp files automatically, as long as they were created within the Transaction scope.

Murray Hertz
  • 112
  • 1
  • 5
0

Here you need to dispose the object of MailMessage.

For Ex.

// Sends email using SMTP with default network credentials
public static void SendEmailToCustomer(string To, string From, string BCC, string    Subject, string Body, bool IsBodyHtml, string attachedPath = "") {

    //create mail message
    MailMessage message = !string.IsNullOrEmpty(From) ? new MailMessage(From, To) : new MailMessage(From, To);

    //create mail client and send email
    SmtpClient emailClient = new SmtpClient();

    //here write your smtp details below before sending the mail.
    emailClient.Send(message);

    //Here you can dispose it after sending the mail
    message.Dispose();

    //Delete specific file after sending mail to customer
    if (!string.IsNullOrEmpty(attachedPath))
        DeleteAttachedFile(attachedPath);
}

//Method to delete attached file from specific path.
private static void DeleteAttachedFile(string attachedPath) {
    File.SetAttributes(attachedPath, FileAttributes.Normal);
    File.Delete(attachedPath);
}