wpf中根据精准文件路径和部分文件名模糊匹配找到文件并打开

文件的存放路径固定:\\10.16.64.30\Technostring\

文件的名称是个变量并且只能收到部分文件名

根据确定的文件存放路径和变动的部分文件名查找到相应的文件并打开

代码如下:

 public ReportDetail(ReportDataModel reportData)
 {
     InitializeComponent();
     vm = new ReportDetailViewModel(this, reportData);
     DataContext = vm;
     coilId = reportData.COIL_ID;
     alloyId = reportData.ALLOY_ID;
//reportData.IBA_PATH: 'ID_25212202_A110y_302_Width_1470_From_0.600_Middle_0.600_To_0.400_250221'
     int lastUnderscoreIndex = reportData.IBA_PATH.LastIndexOf('_');
     if (lastUnderscoreIndex != -1)
     {
         string remainingPart = reportData.IBA_PATH.Substring(0, lastUnderscoreIndex); // 剩余部分
         string lastPart = reportData.IBA_PATH.Substring(lastUnderscoreIndex + 1);     // 最后部分(250221)
         ibaTime = lastPart + "\\";
         ibaPath = remainingPart;
     }

 }

 // 精准路径(固定目录)
 string directoryPath = @"\\10.16.64.30\Technostring\" + ibaTime;

 string safeSearchKey = Path.GetFileName(ibaPath);
 string searchPattern = $"*{safeSearchKey}*.dat";

 LoggingDriver.info("ibaPath is :{0}", ibaPath);
 LoggingDriver.info("directoryPath is :{0}", directoryPath);

 //string filePath = @"\\10.16.64.30\Technostring\" + ibaTime + "*{ ibaPath}* .dat";
 //MessageBox.Show("filePath is " + filePath);
 try
 {
     //Process.Start(filePath);
     // 检查目录是否存在
     if (!Directory.Exists(directoryPath))
     {
         MessageBox.Show("目录不存在!");
         return;
     }

     // 获取匹配的文件列表
     string[] matchedFiles = Directory.GetFiles(directoryPath, searchPattern);

     if (matchedFiles.Length == 0)
     {
         MessageBox.Show("未找到匹配的文件!");
         return;
     }

     // 打开第一个匹配的文件(可根据需求遍历打开多个)
     string targetFile = matchedFiles[0];
     Process.Start(targetFile);
 }
 catch (Exception ex)
 {
     MessageBox.Show($"Error opening file: {ex.Message}");
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值