当前位置: 首页 > news >正文

【31】C#实战篇——获取路径下的文件名(不包含路径和扩展名),并分离出文件名`fileName` ,文件名编号`SN`,文件名前缀`WMT`

文章目录

  • 1 要求
  • 2 分析
  • 3 代码实现及验证

1 要求

写一个函数,获取路径下的文件名(不包含路径和扩展名),并分离出文件名fileName ,文件名编号SN,文件名前缀WMT

输入文件路径,解析出不带".“后缀的文件名fileName,然后fileName进一步拆分为SN+WMF 格式,其中WMT是MatchFilter数组中去掉”."后缀的部分,那么 fileName 减去末尾的WMF就得到SN。

例如

“E:\MTF\A1\StandardData\00000000000001Tele.csv”
拆分结果 fileName = 00000000000001Tele,SN = 00000000000001,WMT =Tele;

观察发现文件名SN部分,除了不光是数字编号,有时还有字母夹杂,这就增加了难度。

“E:\MTF\A1\StandardData\00ac0000FG0001AWide.csv”
拆分结果 fileName = 00ac0000FG0001AWide,SN = 00ac0000FG0001A,WMT =Wide;

2 分析

观察发现文件名SN部分,除了不光是数字编号,有时是是有字母加载。但文件名后末尾字母是可以指定的。这文件名末尾比如固定为 "Wide", "Tele", "Mid" 中三种情况,那么就可很多好的将SN和WMT 拆分了。

给定过滤匹配条件
string[] MatchFilter = { "Wide.csv", "Tele.csv", "Mid.csv" };”,或者 string[] MatchFilter = { "Wide.xlsx", "Tele.xlsx", "Mid.xlsx" };

写一个函数private void ExtractFileNameParts(string filePath, string[] MatchFilter,ref string fileName,ref string SN, ref string WMT);

3 代码实现及验证

private void ExtractFileNameParts(string filePath, string[] MatchFilter, ref string fileName, ref string SN, ref string WMT)
{// 获取文件名(不包含路径和扩展名)fileName = Path.GetFileNameWithoutExtension(filePath);// 遍历匹配过滤条件foreach (var filter in MatchFilter){// 去掉过滤条件中的扩展名部分string filterWithoutExtension = filter.Substring(0, filter.LastIndexOf('.'));// 检查文件名是否以当前过滤条件的前缀结尾(不区分大小写)if (fileName.EndsWith(filterWithoutExtension, StringComparison.OrdinalIgnoreCase)){// 确定WMT和SNWMT = filterWithoutExtension; // 文件类型部分SN = fileName.Substring(0, fileName.Length - filterWithoutExtension.Length); // 剩下的部分// 结束循环,已找到匹配条件break;}}
}

foreach (var filter in MatchFilter)重点解释

  • foreach 循环遍历 MatchFilter 数组中的每个过滤条件 filter。
  • filter.Substring(0, filter.LastIndexOf('.')) 从过滤条件中去掉扩展名部分,例如 Wide.csv 会变成 Wide。
  • if (fileName.EndsWith(filterWithoutExtension, StringComparison.OrdinalIgnoreCase)) 检查 fileName 是否以过滤条件的前缀结尾(不区分大小写)。
  • 如果匹配成功,将 filterWithoutExtension 赋给 WMT,并从 fileName 中去掉 filterWithoutExtension 部分得到 SN。

调用示例

// 示例调用
string filePath = @"E:\MTF\A1\StandardData\00000000000001Tele.csv";
string[] MatchFilter = { "Wide.csv", "Tele.csv", "Mid.csv" };
string fileName = "";
string SN = "";
string WMT = "";ExtractFileNameParts(filePath, MatchFilter, ref fileName, ref SN, ref WMT);Console.WriteLine("fileName: " + fileName);
Console.WriteLine("SN: " + SN);
Console.WriteLine("WMT: " + WMT);

输出结果应为:

fileName: 00000000000001Tele
SN: 00000000000001
WMT: Tele
http://www.lryc.cn/news/615458.html

相关文章:

  • 智能情趣设备、爆 bug:可被远程操控。。。
  • GPT-5深度解析:革命性AI模型的全面报告与实战指南
  • Linux Makefile解析
  • 车流高峰漏检率↓85%!陌讯时序建模方案在智慧交通的实时优化​
  • Netbsd安装使用
  • Ubuntu下搭建LVGL模拟器
  • [SC]高效地调试SystemC模型中的语法错误
  • actuary notes[1]
  • urmom damn the jvm
  • C++2024 年一级
  • 基于 InfluxDB 的服务器性能监控系统实战(一)
  • P1053 [NOIP 2005 提高组] 篝火晚会
  • Linux学习--软件编程(shell命令)
  • 多线程(四) --- 线程安全问题
  • 使用 Ansys Discovery 进行动态设计和分析
  • js零基础入门
  • HashTable, HashMap, ConcurrentHashMap
  • Java 8 特性
  • 力扣(删除有序数组中的重复项I/II)
  • 20250808组题总结
  • 力扣 hot100 Day70
  • 力扣-35.搜索插入位置
  • SwiftUI 登录页面键盘约束冲突与卡顿优化全攻略
  • AI推理的“灵魂五问”:直面2025算力鸿沟与中国的破局之路
  • Java基础语法全面解析:从入门到掌握
  • MySQL 复制表详细说明
  • 三极管在电路中的应用
  • SpringSecurity过滤器链全解析
  • 工具箱许愿墙项目发布
  • Redis 事务机制