c#-FileInfo.Delete中的异常-IOException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-FileInfo.Delete中的异常-IOException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3501字,纯文字阅读大概需要6分钟。
内容图文
![c#-FileInfo.Delete中的异常-IOException](/upload/InfoBanner/zyjiaocheng/659/1b8b920b737048579ba87e90d1d0ae89.jpg)
对于图库WPF应用程序,会发生这种情况,该应用程序在列表框中显示缩略图,并使用xaml显示选定的图像
问题-有没有一种方法可以安全地删除当前选择的图像而不会得到IOException?
仅供参考-用户拥有目录的所有权限,没有其他进程正在使用该文件
这是删除文件的代码
void deleteCurrentlySelectedImageClick()
{
var o = SelectedPhoto; // class MyPhoto with a string FilePath
string path = o.FilePath;
// ViewList is of type List<MyPhoto> bound to listbox for displaying thumbnails
// for the purists
// it can/should be of type ObservableCollection<>
// but this doesn't change that often here :)
ViewList.Remove(o);
o = null;
firePropertyChanged("ViewList");
firePropertyChanged("SelectedPhoto");
FileInfo fi = new FileInfo(path);
fi.Delete();
}
例外是
System.IO.IOException was unhandled by user code
HResult=-2147024864
Message=The process cannot access the file 'C:\<path>\US-wp2.jpg' because it is being used by another process.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileInfo.Delete()
缩略图的XAML是
<ListBox
IsSynchronizedWithCurrentItem="True"
Name="PhotosListBox"
Style="{StaticResource PhotoListBoxStyle}"
Margin="5"
SelectionMode="Extended"
ItemsSource="{Binding ViewList}"
SelectedIndex="0"
SelectionChanged="PhotosListBox_SelectionChanged"
>
…. //资源xaml是….
<DataTemplate DataType="{x:Type sample1:MyPhoto}">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center" Margin="6">
<!-- Drop Shadow -->
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="4" Background="#44000000">
<Border.RenderTransform>
<TranslateTransform X="5" Y="5" />
</Border.RenderTransform>
<Border.BitmapEffect>
<BlurBitmapEffect Radius="8" />
</Border.BitmapEffect>
</Border>
<!-- Image Template -->
<Border Padding="4" Background="White" BorderBrush="#22000000" BorderThickness="1">
<StackPanel Orientation="Vertical">
<Image Source="{Binding FilePath}"/>
</StackPanel>
</Border>
</Grid>
</DataTemplate>
<!-- Main photo catalog view -->
<Style TargetType="{x:Type ListBox}" x:Key="PhotoListBoxStyle">
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}" >
<WrapPanel Margin="5" IsItemsHost="True" Orientation="Horizontal"
ItemHeight="{Binding ElementName=ZoomSlider, Path='Value'}"
ItemWidth="{Binding ElementName=ZoomSlider, Path='Value'}"
VerticalAlignment="Top" HorizontalAlignment="Stretch" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Style for an individual generic item -->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}" >
<Border SnapsToDevicePixels="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{TemplateBinding Background}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#445B6249" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
完整尺寸的图像xaml很简单
<Image Source="{Binding SelectedPhoto.FilePath}" HorizontalAlignment="Center" VerticalAlignment="Center" />
解决方法:
您的问题的答案是:不,没有删除文件的安全方法,您需要处理IOException.可能有多种原因,例如:
>您可能无权删除文件
>文件被另一个进程使用(有人/其他打开了文件)
>该文件已被删除并且不存在
在您的特定情况下,请检查以下问题:Problems overwriting (re-saving) image when it was set as image source
内容总结
以上是互联网集市为您收集整理的c#-FileInfo.Delete中的异常-IOException全部内容,希望文章能够帮你解决c#-FileInfo.Delete中的异常-IOException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。