gpt4 book ai didi

c# - 搜索并匹配前 6 个字符的 pdf 文件,并在 Windows 默认程序中打开它

转载 作者:太空宇宙 更新时间:2023-11-03 22:55:26 24 4
gpt4 key购买 nike

我是 C# 和编程的新手。我会尽量把问题问清楚,

我将这些 pdf 文件放在一个文件夹中,我想做的是将 pdf 文件的名称与字符串的前 6 个字母匹配,如果它存在,我想在 Windows 默认程序中打开它。例如,我将这些文件放在一个文件夹中,每个 pdf 文件的名称都超过 6 个字母

  • 87163437KJH.pdf
  • 479562087gd.pdf
  • 6489754378.pdf
  • 571283874hgj.pdf
  • 17236897467hdgr.pdf
  • gefreg6834.pdf

我有这个字符串“871634e3khsskdjfh”,我想取前 6 个字母“871634”并搜索与前 6 个字母匹配的 pdf 文件。如果它存在我想在 Windows 默认应用程序中打开它。在这种情况下,“871634”与 87163437KJH.pdf 文件匹配,它应该打开

另一个例子;我有这个字符串“gefreg6df2352345”所以字符串的前 6 个字母是“gefreg”它与 gefreg6834.pdf 文件匹配所以它应该打开那个 pdf。

我尝试使用这种方法,但我无法将这些东西放在一起,请帮忙

string[] files = System.IO.Directory.GetFiles(@"J:\", txt_partnum.Text.Substring(0,6) +"*"+".pdf", System.IO.SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
Process.Start(@"J:\" + txt_partnum.Text.Substring(0,6)+"*"+".pdf");

}
else
{
MessageBox.Show("Drawing doesnot exist in SE Drive !");
}

最佳答案

你快到了。只需要遍历文件:

string[] files = System.IO.Directory.GetFiles(@"J:\", txt_partnum.Text.Substring(0,6) +"*"+".pdf", System.IO.SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
Proccess.Start(file);
}

关于c# - 搜索并匹配前 6 个字符的 pdf 文件,并在 Windows 默认程序中打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45635358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com