1.解释说明
- 这个可以搭配文件下载来一起使用,方便管理下载的资源
- 该程序包含xaml部分和cs部分,这里将分开来进行说明
- 该程序有双击打开对应资源的功能
2.xaml程序示例
- <ListView.View>为要显示的视图,这里是显示文件的名字和日期
- <ListView.ContextMenu>为右键鼠标的菜单属性,这里有个文件打开和删除功能
- 这里要注意的是MouseLeftButtonUp鼠标左键弹起事件,当时设置的鼠标左键按下,但是没有生效,所以用的弹起事件
<ListView Grid.Column="0" x:Name="fileListView" MouseDoubleClick="FileListView_MouseDoubleClick" MouseLeftButtonUp="ViewPreview">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="文件名" Width="280"/>
<GridViewColumn DisplayMemberBinding="{Binding CreationTime}" Header="日期" Width="150"/>
</GridView>
</ListView.View>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="打开" Click="OpenMenuItem_Click"/>
<MenuItem Header="删除" Click="DeleteMenuItem_Click"/>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
3.cs程序示例
- LoadFilesInFolder为加载文件夹的功能,这里的folderPath写上对应文件夹的地址即可,例如LoadFilesInFolder("C:\\User\\View");
- ViewPreview这里自定义了一个文件缩略图预览功能,可以展示对应的png和mp4文件
- FileListView_MouseDoubleClick为鼠标的双击事件,这里调用的打开文件功能
- OpenMenuItem_Click为右键菜单的打开事件
- DeleteMenuItem_Click为右键删除功能,这里删除文件后要更新文件夹
- OpenFile为多次调用的打开文件事件
private void LoadFilesInFolder(string folderPath)
{
if (Directory.Exists(folderPath))
{
var files = Directory.GetFiles(folderPath, "*.*").Where(f => f.EndsWith(".mp4") || f.EndsWith(".png"));
fileListView.ItemsSource = files.Select(f => new FileInfo(f)).ToList();
}
else
{
MessageBox.Show("文件夹不存在!");
}
}
private void ViewPreview(object sender, MouseButtonEventArgs e)
{
var fileInfo = fileListView.SelectedItem as FileInfo;
if (fileInfo != null)
{
try
{
string fileExtension = System.IO.Path.GetExtension(fileInfo.FullName).ToLowerInvariant();
switch (fileExtension)
{
case ".mp4":
newvideo.Visibility = Visibility.Visible;
newvideo.Play();
newvideo.Source = new Uri(fileInfo.FullName, UriKind.Absolute);
newpicture.Visibility = Visibility.Hidden;
break;
case ".png":
newpicture.Visibility= Visibility.Visible;
newpicture.Source = new BitmapImage(new Uri(fileInfo.FullName, UriKind.Absolute));
newvideo.Pause();
newvideo.Visibility = Visibility.Hidden;
break;
default:
MessageBox.Show($"Unsupported file type: {fileExtension}");
break;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error opening file: {ex.Message}");
}
}
}
private void FileListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var fileInfo = fileListView.SelectedItem as FileInfo;
if (fileInfo != null)
{
OpenFile(fileInfo.FullName);
}
}
private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
{
var fileInfo = fileListView.SelectedItem as FileInfo;
if (fileInfo != null)
{
OpenFile(fileInfo.FullName);
}
}
private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
{
var fileInfo = fileListView.SelectedItem as FileInfo;
if (fileInfo != null)
{
string message = $"你确定删除 '{fileInfo.Name}'?";
MessageBoxResult result = MessageBox.Show(message, "Delete Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
try
{
System.IO.File.Delete(fileInfo.FullName);
var observableCollection = fileListView.ItemsSource as ObservableCollection<FileInfo>;
if (observableCollection != null)
{
observableCollection.Remove(fileInfo);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error deleting file: {ex.Message}");
}
}
}
else
{
MessageBox.Show("没有选择要删除的文件!");
}
LoadFilesInFolder("C:\\User\\View");
}
private void OpenFile(string filePath)
{
try
{
string fileExtension = System.IO.Path.GetExtension(filePath).ToLowerInvariant();
switch (fileExtension)
{
case ".mp4":
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = filePath;
processStartInfo.UseShellExecute = true; // Allows the operating system to handle the specified file.
processStartInfo.Verb = "open"; // The default verb is "open".
Process.Start(processStartInfo);
break;
case ".png":
ProcessStartInfo eprocessStartInfo = new ProcessStartInfo();
eprocessStartInfo.FileName = filePath;
eprocessStartInfo.UseShellExecute = true; // Allows the operating system to handle the specified file.
eprocessStartInfo.Verb = "open"; // The default verb is "open".
Process.Start(eprocessStartInfo);
break;
default:
MessageBox.Show($"Unsupported file type: {fileExtension}");
break;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error opening file: {ex.Message}");
}
}