文件的存放路径固定:\\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}");
}