5 Replies - 437 Views - Last Post: 22 July 2013 - 09:10 AM Rate Topic: -----

#1 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Can't delete file because it's used by my application?

Posted 22 July 2013 - 08:21 AM

Hi,

I have problem when i tried to delete picture which is used by my application

here is pic:
Attached Image

here is delete method code:
public void DeleteFile(string file)
        {
            try
            {
                if (File.Exists(file))
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    FileInfo fi = new FileInfo(file);
                    fi.Delete();
                    MessageBox.Show("Deleted succesfully!");
                }
                else
                    MessageBox.Show("File does not exists!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

This post has been edited by DocNet: 22 July 2013 - 08:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't delete file because it's used by my application?

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Can't delete file because it's used by my application?

Posted 22 July 2013 - 08:24 AM

You need to dispose of the Image object first and the try to delete it, in your code i don't see where you are disposing image object you're trying to delete.
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Can't delete file because it's used by my application?

Posted 22 July 2013 - 08:30 AM

There's also the simple fact that if you use Image.FromFile to load your image, you're going to be creating a file handle that you don't necessarily control when is released:

http://stackoverflow...-open-sometimes

Consider instead using Image.FromStream to load your file:

private static Image GetImage(string path)
{
    Image image;
    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        image = Image.FromStream(fs);
    }
    return image;
}


That's a method you should use to get your image.

On top of that, it's unlikely that you actually need to force GC collection. It's better to leave garbage collection to .NET and not get involved with it if you can help it. If you included that code to try to make it so you can delete your file, please remove it.

More info:

http://stackoverflow...andle-on-a-file
Was This Post Helpful? 1
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Can't delete file because it's used by my application?

Posted 22 July 2013 - 08:38 AM

View PostCurtis Rutland, on 22 July 2013 - 05:30 PM, said:

There's also the simple fact that if you use Image.FromFile to load your image, you're going to be creating a file handle that you don't necessarily control when is released:


I had similar problem when using Image.FromFile, i didn't know about this

Quote

The file remains locked until the Image is disposed.

Was This Post Helpful? 1
  • +
  • -

#5 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Can't delete file because it's used by my application?

Posted 22 July 2013 - 08:52 AM

View PostMichael26, on 22 July 2013 - 08:38 AM, said:

View PostCurtis Rutland, on 22 July 2013 - 05:30 PM, said:

There's also the simple fact that if you use Image.FromFile to load your image, you're going to be creating a file handle that you don't necessarily control when is released:


I had similar problem when using Image.FromFile, i didn't know about this

Quote

The file remains locked until the Image is disposed.



Thanks, now works. :)
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Can't delete file because it's used by my application?

Posted 22 July 2013 - 09:10 AM

View PostMichael26, on 22 July 2013 - 10:38 AM, said:

I had similar problem when using Image.FromFile, i didn't know about this

Quote

The file remains locked until the Image is disposed.


According to one of my SO links, that's not always true. Use it at your own risk. There's no downside to loading the file into memory and creating your image from the stream.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1