加载后删除文件
问题描述:
我已经编写了一个可以使用MS Excel interop动态生成饼图的类。我节省在Windows Temp文件夹这个饼图,我试图删除包含此饼图在成功加载在图像控制权后,该文件夹,迄今:加载后删除文件
private void Tracker() {
Pbar pbar = new Pbar();
Thread t = new Thread(delegate() {
Draw();
Dispatcher.Invoke(delegate()
{
ImageSource IS = new BitmapImage(new Uri(System.IO.Path.GetTempPath() + "PieChartTemp\\Temp_files\\image002.png", UriKind.Absolute)); ImageBox.Source = IS;
});
});
Thread Track = new Thread(delegate() {
t.Start();
Dispatcher.Invoke(() => pbar.Show());
while (true) {
if (!t.IsAlive) { Dispatcher.Invoke(() => pbar.Close());
System.IO.Directory.Delete(System.IO.Path.GetTempPath() + "PieChartTemp", true);
break;
}
Thread.Sleep(25);
}
});
Track.Start();
}
这种方法的原因一个错误,它说该文件正在被一个进程使用(当然是将它加载到图像控件中的进程),我的问题是如何释放这个文件以准备删除它?
答
创建
不要让BitmapImage
做文件访问。相反,
- 文件读入到一个
MemoryStream
- 从
MemoryStream
BitmapImage