我做WPF程序,用一个listbox控件绑定显示一个文件夹中的图片,然后点击图片的时候,另外一个image控件显示点击的图片,然后我设置了一个按钮,点击按钮时,删除listbox选中的图片,为什么删除的时候,提示不让删呢??怎么办,求帮助啊。。
C#代码如下:
读取图片的代码
private void SJ_Mode( )
{
SJ = new List<string>( );
string path= AppDomain.CurrentDomain.BaseDirectory+@"ImageMean\";
strFile=Directory.GetDirectories( path );//获取文件下的文件夹
for ( int j=0 ; j<strFile.Length ; j++ )
{
string name=strFile[j];
DirectoryInfo DInfo = new DirectoryInfo( name );
FileSystemInfo[] FSInfo = DInfo.GetFileSystemInfos( );
for ( int i = 0 ; i < FSInfo.Length ; i++ )
{
string FileType = FSInfo[i].ToString( ).Substring( FSInfo[i].ToString( ).LastIndexOf( "." ) + 1 , ( FSInfo[i].ToString( ).Length - FSInfo[i].ToString( ).LastIndexOf( "." ) - 1 ) );
FileType = FileType.ToLower( );
if ( FileType == "jpg" || FileType == "png" || FileType == "bmp" || FileType == "gif" || FileType == "jpeg" )
{
Image image=new Image( );
image.Width=150;
image.Height=150;
image.Source=new BitmapImage( new Uri( FSInfo[i].FullName.ToString( ) ) );
ListBoxItem listboxItems=new ListBoxItem( );
listboxItems.Content=image;
image.MouseLeftButtonDown+=new MouseButtonEventHandler( image_MouseLeftButtonDown );
list.Items.Add( listboxItems );
}
}
}
}
获取listbox中image的source值,即图片的路径 string delPath;
void image_MouseLeftButtonDown( object sender , MouseButtonEventArgs e )
{
Image image=sender as Image;
delPath=image.Source.ToString( );
}
点击按钮时删除图片的代码
list.Items.Clear( );
path =delPath.Substring( 8 );
File.Delete( path );当程序运行到file.delete的时候出现错误,说图片正在由另一进程使用,这个问题怎么解决??当listbox显示图片的名字的时候,图片则可以正常删除,但绑定图片的时候则不能删除,怎么解决???求帮助啊。
C#代码如下:
读取图片的代码
private void SJ_Mode( )
{
SJ = new List<string>( );
string path= AppDomain.CurrentDomain.BaseDirectory+@"ImageMean\";
strFile=Directory.GetDirectories( path );//获取文件下的文件夹
for ( int j=0 ; j<strFile.Length ; j++ )
{
string name=strFile[j];
DirectoryInfo DInfo = new DirectoryInfo( name );
FileSystemInfo[] FSInfo = DInfo.GetFileSystemInfos( );
for ( int i = 0 ; i < FSInfo.Length ; i++ )
{
string FileType = FSInfo[i].ToString( ).Substring( FSInfo[i].ToString( ).LastIndexOf( "." ) + 1 , ( FSInfo[i].ToString( ).Length - FSInfo[i].ToString( ).LastIndexOf( "." ) - 1 ) );
FileType = FileType.ToLower( );
if ( FileType == "jpg" || FileType == "png" || FileType == "bmp" || FileType == "gif" || FileType == "jpeg" )
{
Image image=new Image( );
image.Width=150;
image.Height=150;
image.Source=new BitmapImage( new Uri( FSInfo[i].FullName.ToString( ) ) );
ListBoxItem listboxItems=new ListBoxItem( );
listboxItems.Content=image;
image.MouseLeftButtonDown+=new MouseButtonEventHandler( image_MouseLeftButtonDown );
list.Items.Add( listboxItems );
}
}
}
}
获取listbox中image的source值,即图片的路径 string delPath;
void image_MouseLeftButtonDown( object sender , MouseButtonEventArgs e )
{
Image image=sender as Image;
delPath=image.Source.ToString( );
}
点击按钮时删除图片的代码
list.Items.Clear( );
path =delPath.Substring( 8 );
File.Delete( path );当程序运行到file.delete的时候出现错误,说图片正在由另一进程使用,这个问题怎么解决??当listbox显示图片的名字的时候,图片则可以正常删除,但绑定图片的时候则不能删除,怎么解决???求帮助啊。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货