-1

I use the below code to get files with different extensions in the folder by LINQ, but no file is found.

var dirInfo = new DirectoryInfo(Application.StartupPath + "\\TextDocument\\");
var filteredFiles = (from fileDir in dirInfo.GetFiles()
                        where (fileDir.Extension == "*.jpg" || fileDir.Extension == "*.rtf")
                        select fileDir).ToList();

foreach (var item in filteredFiles)
{
    listBox1.Items.Add(item);
}

I get filteredFiles.count = 0 in the foreach loop. What's wrong in this code?

4

2 回答 2

3

Please try remove * from *.jpg.

于 2012-09-09T14:11:09.730 回答
1

Try with,

List<System.IO.FileInfo> oFileInfoList = new System.IO.DirectoryInfo(Application.StartupPath + "\\TextDocument\\").GetFiles().Where(o => o.Extension == ".jpg" || o.Extension == ".rtf").ToList<System.IO.FileInfo>();

FileInfo.Extension have extension without * i.e. .jpg, .png, .pdf

Hope It should Help.

于 2012-09-09T14:38:45.540 回答