wpf写一个文件移动的程序, 提供文件目录a,将a盘的指定文件后缀名的文件复制到b目录,需要遍历a盘的函数。程序功能要有打开文件夹的对话框

回答

2023/4/27 06:11:16

以下是一个示例WPF程序,可实现在用户选择的文件夹中遍历并复制指定后缀名的文件到另一个目录。

首先,在XAML文件中创建一个带有“打开文件夹”按钮和文本框的UI。

 

复制代码

<Window x:Class="WpfFileManager.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="File Manager" Height="200" Width="400">

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition/>

</Grid.RowDefinitions>

<StackPanel Orientation="Horizontal" Margin="4">

<Button x:Name="btnOpenFolder" Content="Open Folder" Click="btnOpenFolder_Click" Margin="2"/>

<TextBlock Text="Selected folder:" Margin="2"/>

<TextBox x:Name="txtSelectedFolder" IsReadOnly="True" Margin="2"/>

</StackPanel> <ListBox x:Name="lstFiles" Grid.Row="1" Margin="4"/>

</Grid>

</Window>

接下来,在代码文件中添加以下事件处理程序:

该程序使用了 OpenFileDialog 类,允许用户选择目录并存储在 _selectedFolder 变量中。单击“复制文件”按钮

using System;
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Windows; 
using Microsoft.Win32; 
namespace WpfFileManager
{
    public partial class MainWindow : Window
    {
        private string _selectedFolder = ""; public MainWindow() { InitializeComponent(); }
        private void btnOpenFolder_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                CheckFileExists = false, FileName = "Folder Selection.", Filter = "Folders (*.*)|*.*"
            };
            if (dialog.ShowDialog() == true)
            { _selectedFolder = Path.GetDirectoryName(dialog.FileName); txtSelectedFolder.Text = _selectedFolder;
            }
        }
        private void CopyFiles(string sourceFolder, string destFolder, List<string> extensions)
        {
            foreach (var file in Directory.GetFiles(sourceFolder).Where(file => extensions.Contains(Path.GetExtension(file))))
            { File.Copy(file, Path.Combine(destFolder, Path.GetFileName(file)), true);
            }
            foreach (var subFolder in Directory.GetDirectories(sourceFolder))
            {
                CopyFiles(subFolder, destFolder, extensions);
            }
        }
        private void lstFiles_SelectionChanged(object sender, RoutedEventArgs e)
        {
            var selectedFile = lstFiles.SelectedItem as string; if (!string.IsNullOrEmpty(selectedFile))
            {
                try
                {
                    System.Diagnostics.Process.Start(selectedFile); // Launch the selected file. 
                }
                catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        private void btnCopyFiles_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var extensions = new List<string> { ".docx", ".xlsx", ".pdf" }; // Change to desired file extensions. 
                var filesToCopy = Directory.GetFiles(_selectedFolder, "*.*", SearchOption.AllDirectories).Where(file => extensions.Contains(Path.GetExtension(file)));
                var destFolder = @"C:\DestinationFolder\"; // Change to destination folder path.
                if (!Directory.Exists(destFolder)) { Directory.CreateDirectory(destFolder); }
                foreach (var file in filesToCopy) { File.Copy(file, Path.Combine(destFolder, Path.GetFileName(file)), true);
                }
                MessageBox.Show($"{filesToCopy.Count()} files copied successfully.", "File Manager", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); }
        }
    }
}

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐