S
S
Sergey Karbivnichy2015-03-09 15:31:30
Programming
Sergey Karbivnichy, 2015-03-09 15:31:30

C# After loading a picture in picturebox, delete it from disk?

Loading an image in a picturebox. Then I want to remove it from the disk. But it throws an exception that the file is busy. Already before removal did so:
picturebox1.Image = null;
- does not help. How to be? Thank you.
I load like this:
picturebox1.load(путь_к_картинке);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2015-03-09
@hottabxp

It is better to work with files through FileStream in order to fully control the whole process:

string filePath = "123.jpg";

using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Inheritable))
{
  pictureBox1.Image = Image.FromStream(file);
}

File.Delete(filePath);

Many of the simplified methods for working with files often block access to files. It makes sense to use such methods only for one-time file operations (when you need to read or write data once and no more than that).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question