C# C IEnumerable所需文件

C# C IEnumerable所需文件,c#,C#,我有以下代码: var neededFiles = new[]{ "file1.exe", "file2.exe", "file3.exe", "file4.exe" }; IEnumerable<string> notFound = neededFiles.Where(f => !File.Exists(f)); if (notFound.Any()) { MessageBox.Show(string.Format(notFound.Count() > 1 ?

我有以下代码:

var neededFiles = new[]{ "file1.exe", "file2.exe", "file3.exe", "file4.exe" };

IEnumerable<string> notFound = neededFiles.Where(f => !File.Exists(f));
if (notFound.Any())
{
    MessageBox.Show(string.Format(notFound.Count() > 1 ?
        "App cannot find these files - {0}" :
        "App cannot find this file - {0}",
        String.Join(", ", neededFiles)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    return;
}
它工作正常,但如果我有例如file1.exe、file2.exe、file3.exe文件,应用程序无法找到这些文件-file1.exe、file2.exe、file3.exe、file4.exe。如何使其只打印不存在的所需文件?示例:如果存在file1.exe、file3.exe,则必须打印:应用程序找不到这些文件-file2.exe、file4.exe 提前谢谢


修正:这对我有帮助。

你的打字错误。执行String.Join时,必须将其传递给notFound,而不是所需的文件:


你需要String.Join,notFound,而不是String.Join,NeedFiles。

我已经尝试过了,但它是一样的:这些字符串:App找不到这些文件-{0}:App找不到这个文件-{0}chages,但文件没有:/google翻译。。。2的参数:System.Collections.Generic.IEnumerable的类型转换为字符串[]是不可能的,并且是增压字符串的最合适的方法。Join string,string[]有一些无效的参数。S已修复,它将帮助我:是,notFound.ToArray应该可以工作,但我想知道为什么你没有加入重载,也需要IEnumerable。您可能使用的是较旧的.NET版本?是否确定应用程序的工作目录中有file1.exe、file2.exe和file3.exe?这应该是有效的。
MessageBox.Show(string.Format(notFound.Count() > 1 ?
        "App cannot find these files - {0}" :
        "App cannot find this file - {0}",
        String.Join(", ", notFound)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Warning);