Files
AohCopyRou/FileService.cs
Mr.Xia 0a534e7e08 新增应用程序图标并优化界面与匹配逻辑
- 增加 复制.ico 作为应用图标,并在项目文件中配置 ApplicationIcon。
- 修正 MatchFolderName 数字编号截取索引,保证匹配准确。
- 调整主窗口高度为 645,优化界面体验。
- LogTextBox 允许编辑(去除 IsReadOnly)。
- 处理单个料号时回车后自动聚焦输入框,提升操作流畅度。
- 代码格式优化,提升可读性。
2025-12-29 16:41:44 +08:00

151 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace CopyRou
{
public class FileService : IFileService
{
public bool MatchFolderName(string folderName, string numPart, string versionPart)
{
if (string.IsNullOrEmpty(folderName) ||
string.IsNullOrEmpty(numPart) ||
string.IsNullOrEmpty(versionPart))
return false;
var folderNameLower = folderName.ToLower();
var numLength = numPart.Length;
// 检查文件夹名长度是否足够
if (folderNameLower.Length < 3 + numLength)
return false;
// 从第4个字符开始匹配数字编号索引3
var numInName = folderNameLower.Substring(4, numLength);
if (numInName != numPart.ToLower())
return false;
// 检查版本部分
var versionInName = folderNameLower.Substring(4 + numLength);
return versionInName.StartsWith(versionPart.ToLower());
}
public List<string> CopyRouFiles(string sourceFolder, string number, string destPath, ILogger logger)
{
var copiedFiles = new List<string>();
var edFoldersFound = 0;
var rouExtensions = new[] { ".rou", ".rou1", ".rou2", ".rou3" };
try
{
// 确保目标目录存在
Directory.CreateDirectory(destPath);
foreach (var item in Directory.GetDirectories(sourceFolder))
{
var folderName = Path.GetFileName(item);
if (folderName.StartsWith("ED", StringComparison.OrdinalIgnoreCase) ||
folderName.StartsWith("ROU", StringComparison.OrdinalIgnoreCase))
{
edFoldersFound++;
logger?.Log($"正在处理ED文件夹: {folderName}");
// 递归查找所有.rou文件
var rouFiles = Directory.GetFiles(item, "*.*", SearchOption.AllDirectories)
.Where(file => rouExtensions.Contains(Path.GetExtension(file).ToLower()))
.ToList();
foreach (var srcFile in rouFiles)
{
var fileName = Path.GetFileName(srcFile);
var baseName = $"{number}_{folderName}_{fileName}";
var destFile = Path.Combine(destPath, baseName);
File.Copy(srcFile, destFile, true);
copiedFiles.Add(baseName);
}
}
}
if (edFoldersFound == 0)
{
logger?.LogWarning("未找到任何以ED或ROU开头的文件夹");
}
else if (!copiedFiles.Any())
{
logger?.LogWarning("在ED文件夹中未找到任何.rou系列文件");
}
else
{
logger?.Log($"成功从 {edFoldersFound} 个ED文件夹复制 {copiedFiles.Count} 个文件");
}
}
catch (Exception ex)
{
logger?.LogError($"复制文件时发生错误: {ex.Message}");
}
return copiedFiles;
}
public async Task ProcessCodes(List<string> codes, List<string> sourcePaths, string destPath,
IProgress<int> progress, ILogger logger)
{
var totalCodes = codes.Count;
var processedCodes = 0;
foreach (var code in codes)
{
var trimmedCode = code.Trim();
if (string.IsNullOrEmpty(trimmedCode))
continue;
logger?.Log($"\n处理编号: {trimmedCode}");
var numPart = trimmedCode.Length >= 5 ? trimmedCode.Substring(0, 5) : trimmedCode;
var versionPart = trimmedCode.Length > 5 ? trimmedCode.Substring(5) : "";
var found = false;
foreach (var sourceRoot in sourcePaths)
{
var numFolder = Path.Combine(sourceRoot, numPart);
if (!Directory.Exists(numFolder))
continue;
try
{
var folders = Directory.GetDirectories(numFolder);
foreach (var folder in folders)
{
var folderName = Path.GetFileName(folder);
if (MatchFolderName(folderName, numPart, versionPart))
{
logger?.Log($"找到匹配文件夹: {folder}");
CopyRouFiles(folder, numPart, destPath, logger ?? new Logger());
found = true;
}
}
}
catch (Exception ex)
{
logger?.LogError($"访问文件夹 {numFolder} 时发生错误: {ex.Message}");
}
}
if (!found)
{
logger?.Log("未找到匹配的文件夹");
}
processedCodes++;
progress?.Report((int)((double)processedCodes / totalCodes * 100));
// 添加小延迟以避免UI阻塞
await Task.Delay(10);
}
}
}
}