下载后无法删除文件

时间:2009-11-25 18:00:07

标签: 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();

4 个答案:

答案 0 :(得分:6)

您需要处置Image对象。

  

文件保持锁定状态,直到   图像处理完毕。

答案 1 :(得分:2)

您可以使用ProcessExplorer来检查该文件的未关闭人员。

答案 2 :(得分:0)

听起来你需要首先关闭WebClient,因为它会锁定它。也许使用

WebClient.Close() or WebClient.Dispose()

希望这会给你提示和帮助, 最好的祝福, 汤姆。

答案 3 :(得分:0)

你能展示更多代码吗?好像你是从文件中读取

WebClient Class

您没有关闭流/阅读器。这将保留在文件中,并且不允许您删除。

相关问题