2009-11-25 39 views
0

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

我得到这个错误:

file 'exfile.jpgg' because it is being used by another process.

这里是我的代码,

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(); 
+1

在试图删除它之前,您是否正在对文件进行任何操作?如果是这样,请发布该代码。 –

回答

5

您需要处置Image对象。

The file remains locked until the Image is disposed.

0

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

 
WebClient.Close() or WebClient.Dispose() 

希望这给你的提示和帮助, 最好的问候, 汤姆。

+1

或者使用using指令为了不忘记使用(WebClient w = new WebClient())关闭/处理webclient: w.DownloadFile(“http://file.com/exfile.jpg”,“ exfile.jpg“); } – tobsen

+1

我不相信WebClient有一个Close方法吗? – Dougc

+1

处置无效 – toms

0

你能展示一些更多的代码吗?好像如果你是从文件

WebClient Class

,你是不是关闭流/读取器读取。这将保留该文件,并且不允许您删除。

+0

我试图删除文件,使用此: FileInfo MyFile = new FileInfo(@ exfile.jpg); MyFile.Delete(); 也许这个零件是错误的? – toms

+0

为什么不System.IO.Path.Delete(...)? – t0mm13b

+0

System.IO.Path'不包含'Delete'的定义 – toms