下载后无法删除文件

0 c# file download

为什么我下载后无法删除文件?

我收到此错误:

文件'exfile.jpgg'因为它正由另一个进程使用.

这是我的代码,

string file_name = "pic.jpg"
WebClient client = new WebClient();
client.DownloadFile("http://picture.com/pic.jpg", file_name);
client.Dispose();
client = null;

pictureBox1.Image = Image.FromFile(File_Name);
pictureBox1.Image = null;

FileInfo MyFile = new FileInfo(File_Name);
MyFile.Delete();
Run Code Online (Sandbox Code Playgroud)

小智 6

您需要处置该Image对象.

文件保持锁定状态,直到图像被丢弃.