Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打开文件名包含字符串的pdf文件_C#_Pdf - Fatal编程技术网

C# 打开文件名包含字符串的pdf文件

C# 打开文件名包含字符串的pdf文件,c#,pdf,C#,Pdf,好的,我想打开一个pdf文件,它的名字包含特定的字符串 这是我以前看到的一段代码。是否存在名称包含字符串的文件: if (Directory.EnumerateFiles(startInfo.Arguments).Any(PDFfile=>PDFfile.Contains(myString))) { MessageBox.Show("Jackpot"); } else { MessageBox.Show("There is no file!"); } 我收到“Jackpo

好的,我想打开一个pdf文件,它的名字包含特定的字符串

这是我以前看到的一段代码。是否存在名称包含字符串的文件:

if (Directory.EnumerateFiles(startInfo.Arguments).Any(PDFfile=>PDFfile.Contains(myString)))
{
    MessageBox.Show("Jackpot");
}
else
{
    MessageBox.Show("There is no file!");
}
我收到“Jackpot”消息,现在如何打开该文件?!我知道我应该使用:

Process.Start(startInfo);

但这是在您精确指定了文件路径的情况下进行的。

您可以通过几种方法来完成,其中一种方法是:

var filesInDirectory = Directory.EnumerateFiles(startInfo.Arguments);
var pdfFile = filesInDirectory.FirstOrDefault(PDFfile=>PDFfile.Contains(myString));
var pdfFileExists = pdfFile != null;

if (pdfFileExists)
{
    MessageBox.Show("Jackpot");
    Process.Start(pdfFile);
}
else
{
    MessageBox.Show("There is no file!");
}

请注意,
FirstOrDefault
将导致它获取它得到的第一个结果,不确定这是否是您要寻找的结果。

对它进行了一点修改,它就像一个符咒一样工作!!!谢谢你,阿伦,你帮了大忙!