1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
MSDNのサンプルそのままだけど、ちょっと便利。 private void Form1_Load(object sender, EventArgs e) { string startFolder = @"C:\My Documents\My Pictures\"; // Take a snapshot of the file system. IEnumerable fileList = GetFiles(startFolder); //Create the query IEnumerable fileQuery = from file in fileList where file.Extension == ".jpg" orderby file.Name select file; // Create and execute a new query by using the previous // query as a starting point. fileQuery is not // executed again until the call to Last() var files = (from file in fileQuery orderby file.CreationTime select new { file.FullName, file.CreationTime }); } // This method assumes that the application has discovery // permissions for all folders under the specified path. static IEnumerable GetFiles(string path) { if (!System.IO.Directory.Exists(path)) throw new System.IO.DirectoryNotFoundException(); string[] fileNames = null; List files = new List(); fileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); foreach (string name in fileNames) { files.Add(new FileInfo(name)); } return files; } |
FileInfoのIEnumerable持ってくるなら、@ITで川俣さんが書いてるサンプルが美しいと思うですよ。http://www.atmarkit.co.jp/fdotnet/csharp30/csharp30_06/csharp30_06_01.htmlあ、元ネタは同じかな。