namespace DragDropTextFilesApp
{
public partial class MainWindow : Window
{
private readonly SolidColorBrush NormalBorderBrush = new SolidColorBrush(Color.FromRgb(153, 153, 153)); // #999999
private readonly SolidColorBrush HighlightBorderBrush = new SolidColorBrush(Color.FromRgb(0, 122, 204)); // 蓝色高亮
code Copy code
public MainWindow()
{
InitializeComponent();
StatusText.Text = "请将包含文本文档的文件夹或多个 .txt 文件拖放到上方区域。";
DropArea.BorderBrush = NormalBorderBrush;
}
private void DropArea_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 && paths.All(path => Directory.Exists(path) || IsTextFile(path)))
{
e.Effects = DragDropEffects.Copy;
AnimateBorderColor(DropArea, HighlightBorderBrush);
}
else
{
e.Effects = DragDropEffects.None;
AnimateBorderColor(DropArea, NormalBorderBrush);
}
}
else
{
e.Effects = DragDropEffects.None;
AnimateBorderColor(DropArea, NormalBorderBrush);
}
e.Handled = true;
}
private void DropArea_Drop(object sender, DragEventArgs e)
{
AnimateBorderColor(DropArea, NormalBorderBrush);
StatusText.Text = "正在处理文件...";
ContentRichTextBox.Visibility = Visibility.Collapsed;
try
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
{
string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop, true);
if (paths.Length == 0)
{
StatusText.Text = "未检测到任何文件或文件夹。";
return;
}
// 验证所有拖放项是否有效
if (!paths.All(path => Directory.Exists(path) || IsTextFile(path)))
{
MessageBox.Show("拖放的内容必须是文件夹或 .txt 文件。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
StatusText.Text = "无效的拖放内容。";
return;
}
// 收集所有要处理的 .txt 文件
var allTxtFiles = new System.Collections.Generic.List<string>();
foreach (var path in paths)
{
if (Directory.Exists(path))
{
// 获取文件夹中的所有 .txt 文件(不包括子文件夹)
var txtFilesInDir = Directory.GetFiles(path, "*.txt", SearchOption.TopDirectoryOnly);
allTxtFiles.AddRange(txtFilesInDir);
}
else if (IsTextFile(path))
{
allTxtFiles.Add(path);
}
}
if (allTxtFiles.Count == 0)
{
MessageBox.Show("所选内容中没有找到 .txt 文档。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
StatusText.Text = "未找到 .txt 文件。";
return;
}
// 按拖放顺序排序文件
var orderedFiles = allTxtFiles.OrderBy(f => GetDropOrder(paths, f)).ToList();
StringBuilder sb = new StringBuilder();
foreach (var file in orderedFiles.Distinct())
{
sb.AppendLine($"--- 文件: {System.IO.Path.GetFileName(file)} ---");
sb.AppendLine(File.ReadAllText(file));
sb.AppendLine();
}
// 使用 RichTextBox 显示内容
ContentRichTextBox.Document.Blocks.Clear();
ContentRichTextBox.Document.Blocks.Add(new Paragraph(new Run(sb.ToString())));
ContentRichTextBox.Visibility = Visibility.Visible;
StatusText.Text = $"成功加载 {allTxtFiles.Count} 个文件。";
}
}
catch (Exception ex)
{
MessageBox.Show($"处理文件时发生错误:\n{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
StatusText.Text = "发生错误。";
}
}
/// <summary>
/// 检查文件是否为 .txt 文件
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>是否为 .txt 文件</returns>
private bool IsTextFile(string path)
{
return File.Exists(path) && string.Equals(Path.GetExtension(path), ".txt", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// 动画切换边框颜色
/// </summary>
/// <param name="border">目标边框</param>
/// <param name="toColor">目标颜色</param>
private void AnimateBorderColor(Border border, SolidColorBrush toColor)
{
ColorAnimation colorAnimation = new ColorAnimation
{
To = toColor.Color,
Duration = TimeSpan.FromMilliseconds(200)
};
border.BorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
}
/// <summary>
/// 获取文件在拖放项中的顺序
/// </summary>
/// <param name="paths">拖放的路径数组</param>
/// <param name="filePath">文件路径</param>
/// <returns>文件的拖放顺序索引</returns>
private int GetDropOrder(string[] paths, string filePath)
{
for (int i = 0; i < paths.Length; i++)
{
if (filePath.StartsWith(paths[i], StringComparison.OrdinalIgnoreCase))
{
return i;
}
}
return paths.Length; // 默认放在最后
}
}
}严重性 代码 说明 项目 文件 行 禁止显示状态
错误 XDG0000 意外出现标记“Grid”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 结束 XML 标记“Grid”不匹配。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“Window”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“RichTextBox”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“>”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“>”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“>”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“</”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“</”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“</”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“
Visibility="Collapsed">
”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“
SelectionBrush="LightBlue"
”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 意外出现标记“
Foreground="White"
”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 2
错误 XDG0000 意外出现标记“
WindowStartupLocation="CenterScreen"
AllowsTransparency="False"
ResizeMode="CanResize">
”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 7
错误 XLS0112 应为“>”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 7
错误 XLS0112 应为“>”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 24
错误 XLS0112 应为“>”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 55
错误 XLS0501 已多次设置属性“Content”。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 11
错误 XDG0000 属性名称丢失。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 属性名称丢失。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 1
错误 XDG0000 属性名称丢失。 代码抓鱼器 H:\编程测试\编程测试\代码抓鱼器\代码抓鱼器\MainWindow.xaml 7
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace DragDropTextFilesApp
{
public partial class MainWindow : Window
{
private readonly SolidColorBrush NormalBorderBrush = new SolidColorBrush(Color.FromRgb(153, 153, 153)); // #999999
private readonly SolidColorBrush HighlightBorderBrush = new SolidColorBrush(Color.FromRgb(0, 122, 204)); // 蓝色高亮
public MainWindow()
{
InitializeComponent();
StatusText.Text = "请将包含文本文档的文件夹或多个 .txt 文件拖放到上方区域。";
DropArea.BorderBrush = NormalBorderBrush;
}
private void DropArea_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 && paths.All(path => Directory.Exists(path) || IsTextFile(path)))
{
e.Effects = DragDropEffects.Copy;
AnimateBorderColor(DropArea, HighlightBorderBrush);
}
else
{
e.Effects = DragDropEffects.None;
AnimateBorderColor(DropArea, NormalBorderBrush);
}
}
else
{
e.Effects = DragDropEffects.None;
AnimateBorderColor(DropArea, NormalBorderBrush);
}
e.Handled = true;
}
private void DropArea_Drop(object sender, DragEventArgs e)
{
AnimateBorderColor(DropArea, NormalBorderBrush);
StatusText.Text = "正在处理文件...";
ContentRichTextBox.Visibility = Visibility.Collapsed;
try
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
{
string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop, true);
if (paths.Length == 0)
{
StatusText.Text = "未检测到任何文件或文件夹。";
return;
}
// 验证所有拖放项是否有效
if (!paths.All(path => Directory.Exists(path) || IsTextFile(path)))
{
MessageBox.Show("拖放的内容必须是文件夹或 .txt 文件。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
StatusText.Text = "无效的拖放内容。";
return;
}
// 收集所有要处理的 .txt 文件
var allTxtFiles = new System.Collections.Generic.List<string>();
foreach (var path in paths)
{
if (Directory.Exists(path))
{
// 获取文件夹中的所有 .txt 文件(不包括子文件夹)
var txtFilesInDir = Directory.GetFiles(path, "*.txt", SearchOption.TopDirectoryOnly);
allTxtFiles.AddRange(txtFilesInDir);
}
else if (IsTextFile(path))
{
allTxtFiles.Add(path);
}
}
if (allTxtFiles.Count == 0)
{
MessageBox.Show("所选内容中没有找到 .txt 文档。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
StatusText.Text = "未找到 .txt 文件。";
return;
}
// 按拖放顺序排序文件
var orderedFiles = allTxtFiles.OrderBy(f => GetDropOrder(paths, f)).ToList();
StringBuilder sb = new StringBuilder();
foreach (var file in orderedFiles.Distinct())
{
sb.AppendLine($"--- 文件: {System.IO.Path.GetFileName(file)} ---");
sb.AppendLine(File.ReadAllText(file));
sb.AppendLine();
}
// 使用 RichTextBox 显示内容
ContentRichTextBox.Document.Blocks.Clear();
ContentRichTextBox.Document.Blocks.Add(new Paragraph(new Run(sb.ToString())));
ContentRichTextBox.Visibility = Visibility.Visible;
StatusText.Text = $"成功加载 {allTxtFiles.Count} 个文件。";
}
}
catch (Exception ex)
{
MessageBox.Show($"处理文件时发生错误:\n{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
StatusText.Text = "发生错误。";
}
}
/// <summary>
/// 检查文件是否为 .txt 文件
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>是否为 .txt 文件</returns>
private bool IsTextFile(string path)
{
return File.Exists(path) && string.Equals(Path.GetExtension(path), ".txt", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// 动画切换边框颜色
/// </summary>
/// <param name="border">目标边框</param>
/// <param name="toColor">目标颜色</param>
private void AnimateBorderColor(Border border, SolidColorBrush toColor)
{
ColorAnimation colorAnimation = new ColorAnimation
{
To = toColor.Color,
Duration = TimeSpan.FromMilliseconds(200)
};
border.BorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
}
/// <summary>
/// 获取文件在拖放项中的顺序
/// </summary>
/// <param name="paths">拖放的路径数组</param>
/// <param name="filePath">文件路径</param>
/// <returns>文件的拖放顺序索引</returns>
private int GetDropOrder(string[] paths, string filePath)
{
for (int i = 0; i < paths.Length; i++)
{
if (filePath.StartsWith(paths[i], StringComparison.OrdinalIgnoreCase))
{
return i;
}
}
return paths.Length; // 默认放在最后
}
}
}