Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

WPF CS NET4.8 写一个APP 支持鼠标拖入文件夹的多个文本文档,并且能够把不同文档内容顺序在GUI可以被复制文本. 给我写出来

  1. User

    WPF CS NET4.8 写一个APP 支持鼠标拖入文件夹的多个文本文档,并且能够把不同文档内容顺序在GUI可以被复制文本. 给我写出来

  2. Assistant

    当然,以下是一个使用 WPF(Windows Presentation Foundation)和 C#(针对 .NET Framework 4.8)编写的应用程序示例。该应用程序支持将包含多个文本文档的文件夹拖入,并按顺序读取不同文档的内容,显示在 GUI 界面中,用户可以复制这些文本。

    项目设置

    1. 创建项目:

      • 打开 Visual Studio。
      • 选择 新建项目。
      • 选择 WPF 应用程序 (.NET Framework),命名为例如 DragDropTextFilesApp,并确保目标框架设置为 .NET Framework 4.8。
    2. 主窗口设计: 我们将在主窗口中添加一个 TextBox 来显示文本内容,并启用拖放操作。

    XAML 代码(MainWindow.xaml)

    <Window x:Class="DragDropTextFilesApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:DragDropTextFilesApp"
            Title="文本文件合并器" Height="450" Width="800"
            AllowDrop="True"
            DragOver="Window_DragOver"
            Drop="Window_Drop">
        <Grid Margin="10">
            <TextBox x:Name="ContentTextBox"
                     VerticalScrollBarVisibility="Auto"
                     HorizontalScrollBarVisibility="Auto"
                     TextWrapping="Wrap"
                     IsReadOnly="True"
                     AcceptsReturn="True"
                     FontFamily="Consolas"
                     FontSize="14"/>
        </Grid>
    </Window>
    

    代码后置(MainWindow.xaml.cs)

    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Input;
    
    namespace DragDropTextFilesApp
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                // 提示用户拖放文件夹
                ContentTextBox.Text = "请将包含文本文档的文件夹拖放到此窗口。";
            }
    
            private void Window_DragOver(object sender, DragEventArgs e)
            {
                // 检查拖入的数据是否为文件夹
                if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
                {
                    string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                    if (paths.Length > 0 && Directory.Exists(paths[0]))
                    {
                        e.Effects = DragDropEffects.Copy;
                    }
                    else
                    {
                        e.Effects = DragDropEffects.None;
                    }
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                }
                e.Handled = true;
            }
    
            private void Window_Drop(object sender, DragEventArgs e)
            {
                try
                {
                    if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
                    {
                        string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                        if (paths.Length > 0 && Directory.Exists(paths[0]))
                        {
                            string folderPath = paths[0];
                            // 获取所有的 .txt 文件(包括子文件夹,如需要可修改)
                            string[] txtFiles = Directory.GetFiles(folderPath, "*.txt", SearchOption.TopDirectoryOnly);
    
                            if (txtFiles.Length == 0)
                            {
                                MessageBox.Show("所选文件夹中没有找到 .txt 文档。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                                return;
                            }
    
                            StringBuilder sb = new StringBuilder();
                            foreach (var file in txtFiles.OrderBy(f => f))
                            {
                                sb.AppendLine($"--- 文件: {System.IO.Path.GetFileName(file)} ---");
                                sb.AppendLine(File.ReadAllText(file));
                                sb.AppendLine();
                            }
    
                            ContentTextBox.Text = sb.ToString();
                        }
                        else
                        {
                            MessageBox.Show("请拖放一个有效的文件夹。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"处理文件时发生错误:\n{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
    }
    

    代码说明

    1. XAML 界面:

      • AllowDrop="True":允许窗口接收拖放操作。
      • TextBox 控件用于显示文本内容,设置为只读并支持文本选择和复制。
      • 绑定 DragOver 和 Drop 事件,用于处理拖放操作。
    2. 代码后置:

      • Inicializar:
        • 在窗口初始化时,向用户显示提示信息。
      • Window_DragOver:
        • 检查拖入的数据是否为文件夹。
        • 如果是文件夹,允许复制 (DragDropEffects.Copy);否则,不允许。
      • Window_Drop:
        • 获取拖入的路径,确保是一个文件夹。
        • 获取该文件夹下所有 .txt 文件(可以修改 SearchOption 以包含子文件夹)。
        • 读取每个文本文件的内容,并按顺序将其内容添加到 StringBuilder 中,文件名作为分隔。
        • 将合并后的内容显示在 TextBox 中。
        • 处理任何可能的异常,并向用户显示错误信息。

    运行与使用

    1. 编译并运行应用程序。
    2. 拖放一个包含多个 .txt 文档的文件夹到应用程序窗口。
    3. 应用程序将按顺序读取每个文本文件的内容,并在界面中的 TextBox 中显示出来。
    4. 用户可以在 TextBox 中选择并复制任何文本内容。

    扩展功能(可选)

    • 支持子文件夹:如果希望包括子文件夹中的 .txt 文件,可以将 SearchOption.TopDirectoryOnly 修改为 SearchOption.AllDirectories。
    • 文件排序:当前代码按文件名排序读取文件,可以根据需求调整排序方式,例如按创建时间或自定义顺序。
    • UI 改进:可以添加进度条、状态信息或其他控件以增强用户体验。

    希望这个示例能帮助你快速开发所需的 WPF 应用程序!