我使用下面的代码在我的WPF应用程序显示某些图像使用的文件:无法删除由其他进程
<Image Source="{Binding Path=TemplateImagePath, Mode=TwoWay}" Grid.Row="3" Grid.Column="2" Width="400" Height="200"/>
,并设置它通过一些目录导航结合内部的代码背后的constructor属性,下面是代码:
DirectoryInfo Dir = new DirectoryInfo(@"D:/Template");
if (Dir.Exists)
{
if (Dir.GetFiles().Count() > 0)
{
foreach (FileInfo item in Dir.GetFiles())
{
TemplateImagePath = item.FullName;
}
}
}
但如果用户上传一些其他的图像,然后我需要删除这是我在下面的方式做,并设置图像结合为null这个旧形象:
DirectoryInfo Dir = new DirectoryInfo(@"D:/Template");
if (Dir.Exists)
{
if (Dir.GetFiles().Count() > 0)
{
foreach (FileInfo item in Dir.GetFiles())
{
TemplateImagePath= null;
File.Delete(item.FullName);
}
}
}
但Iam得到异常,无法删除某些其他进程使用的文件。 我该如何删除它?
您是否尝试过不使用双向绑定?可以工作的另一个解决方案是不直接设置路径,而是从路径创建一个BitmapImage并绑定到该位图图像。 – Akku
该怎么做。我是新手在WPF.any代码示例 – SST
对不起,没时间,请使用Google。 – Akku