无法删除由其他进程使用的文件(Cannot delete file used by some oth

2019-06-18 11:59发布

我使用下面的代码在我的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);
                        }
                    }
                }

但是荫得到不同的是无法删除由其他进程使用的文件。 我怎么能删除吗?

Answer 1:

为了能够同时显示在一个ImageControl删除的图像,你必须创建一个新的BitmapImage或BitmapFrame对象有BitmapCacheOption.OnLoad集。 位图将被从文件立即加载该文件事后又没有锁定。

从更改属性string TemplateImagePathImageSource TemplateImage和绑定是这样的:

<Image Source="{Binding TemplateImage}"/>

该设置TemplateImage属性是这样的:

BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(item.FullName);
image.EndInit();
TemplateImage = image;

或这个:

TemplateImage = BitmapFrame.Create(
    new Uri(item.FullName),
    BitmapCreateOptions.None,
    BitmapCacheOption.OnLoad);

如果你想保持绑定到您的TemplateImagePath属性,你可以改为使用绑定转换器 ,该字符串转换为ImageSource的如上图所示。



Answer 2:

据克莱门斯的建议,这里是绑定转换器有一个良好的代码重用:

namespace Controls
{
    [ValueConversion(typeof(String), typeof(ImageSource))]
    public class StringToImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is string valueString))
            {
                return null;
            }
            try
            {
                ImageSource image = BitmapFrame.Create(new Uri(valueString), BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
                return image;
            }
            catch { return null; }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

并且对于绑定字符串,例如

public string MyImageString { get; set; } = @"C:\test.jpg"

而在UI转换器使用,在我的情况下,从命名图书馆“控制”

<Window x:Class="MainFrame"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:Controls;assembly=Controls">
    <Window.Resources>
        <controls:StringToImageSourceConverter x:Key="StringToImageSourceConverter" />
    </Window.Resources>
    <Grid>
        <Image Source="{Binding MyImageString, Converter={StaticResource StringToImageSourceConverter}}" />
    </Grid>
</Window>


文章来源: Cannot delete file used by some other process