private void button4_Click(object sender, EventArgs e) { string searchNum = textBox1.Text.Trim(); string folderPath; folderPath = textBox2.Text.Trim(); foreach (string filePath in Directory.GetFiles(folderPath, "*.pdf")) { string fileName = Path.GetFileNameWithoutExtension(filePath); if (fileName.Length >= 8) { string firstEightDigits = fileName.Substring(0, 8); if (firstEightDigits == searchNum) { Process.Start(filePath); return; // 打开第一个匹配的pdf文件后退出循环 } } } MessageBox.Show("未找到匹配的pdf文件。"); }修改为扫描出二维码在文本框中只显示8位并打开第一个匹配的pdf文件后退出循环
时间: 2024-02-10 21:17:05 浏览: 29
你可以在 `button4_Click` 方法中添加处理扫描结果的逻辑,例如:
1. 获取二维码扫描结果的字符串表示。
2. 截取前8个字符。
3. 将截取后的字符串赋值给 `searchNum` 变量。
4. 遍历文件夹中的 PDF 文件,依次判断文件名是否匹配。
5. 如果匹配成功,则打开该文件并退出循环。
6. 如果遍历完所有文件都没有匹配成功,则弹出提示消息。
示例代码如下:
```csharp
private void button4_Click(object sender, EventArgs e)
{
// 获取扫描结果并截取前8个字符
string scanResult = textBox1.Text.Trim();
string searchNum = scanResult.Substring(0, 8);
// 获取文件夹路径
string folderPath = textBox2.Text.Trim();
// 遍历文件夹中的 PDF 文件
foreach (string filePath in Directory.GetFiles(folderPath, "*.pdf"))
{
// 获取文件名并截取前8个字符
string fileName = Path.GetFileNameWithoutExtension(filePath);
string firstEightDigits = fileName.Substring(0, 8);
// 判断是否匹配成功
if (firstEightDigits == searchNum)
{
// 打开第一个匹配的 PDF 文件并退出循环
Process.Start(filePath);
return;
}
}
// 如果遍历完所有文件都没有匹配成功,则弹出提示消息
MessageBox.Show("未找到匹配的pdf文件。");
}
```
这样,当用户扫描二维码后点击按钮,程序会自动截取前8个字符并进行匹配,只打开第一个匹配的 PDF 文件并退出循环。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)