English 中文(简体)
C# list file in Director- issue
原标题:C# list file in directory issue
  • 时间:2009-09-07 10:37:37
  •  标签:

∗ UNEP/POPS/POPRC.2/1。 iii

最佳回答

类似于


System.IO.FileInfo[] array = new System.IO.DirectoryInfo("directory_path").GetFiles();
Array.Sort(array, delegate(System.IO.FileInfo f1, System.IO.FileInfo f2)
            {
                return f2.CreationTimeUtc.CompareTo(f1.CreationTimeUtc);
            });

问题回答

我也许会使用准则和清单......这样做的工作:

 DirectoryInfo di = new DirectoryInfo("YourPath");
 List<FileInfo> files = di.GetFiles().OrderBy(f => f.CreationTime).ToList();

 foreach (FileInfo file in files)
        {
            //do something
        }

like:

DirectoryInfo di = new DirectoryInfo("path to folder");
FileInfo[] files = di.GetFiles();
IOrderedEnumerable<FileInfo> enumerable = files.OrderBy(f => f.CreationTime);

foreach (FileInfo info in enumerable)
{
  // do stuff...        
}

<>strong>EDIT: Update, here s a non-LINQ Solutions

FileInfo[] files = new DirectoryInfo("directory").GetFiles();
Array.Sort(files, delegate(FileInfo f1, FileInfo f2) {
    return f2.CreationTime.CompareTo(f1.CreationTime);
});

以上将至少老化。 代表:<代码>return f1.Creationtime.CompareTo(f2.Creationtime);


LINQ solution:
FileInfo[] files = new DirectoryInfo("directory").GetFiles();
var results = files.OrderByDescending(file => file.CreationTime);

1. 使用令 通过按最近创建时间进行分类,否则会利用By教令从最老的到最新的创建时间。

DirectoryInfo baseFolder=new DirectoryInfo("folderName");
FileInfo[] files=baseFolder.GetFiles("");
for(int i=1; i<=files.Length;i++)
for(int j=1; j<files.Length;j++)
{
    if(files[j].CreationTime > files[j+1].CreationTime)
    {
        FileInfo f = files[j];
        files[j] = files[j+1];
        files[j+1] = f;
    }
}




相关问题
热门标签