日期:2014-05-18  浏览次数:20853 次

FileInfo[] fi=DirectInfo.GetFiles() 获取的文件在fi中存放顺序?
比如有1.jpg和2.jpg两个文件。哪个是保存在fi[0],哪个保存在fi[1]

------解决方案--------------------
FileInfo[] theFile;
DirectoryInfo theFolder;
string TruePath = Server.MapPath(UpFilesDir);
theFolder = new DirectoryInfo(TruePath );

theFile = FilterForFiles(theFolder.GetFiles());
DataTable FileTab = new DataTable();
DataRow FileRow;
FileTab.Columns.Add(new DataColumn("Name",typeof(string)));
FileTab.Columns.Add(new DataColumn("Extension", typeof(String)));
FileTab.Columns.Add(new DataColumn("Length", typeof(Double)));
FileTab.Columns.Add(new DataColumn("CreationTime", typeof(String)));

foreach (FileInfo tmpFile in theFile)
{
FileRow = FileTab.NewRow();
FileRow[0] = tmpFile.Name;
FileRow[1] = tmpFile.Extension;
FileRow[2] = tmpFile.Length;
FileRow[3] = tmpFile.CreationTime.ToString("yyyy-MM-dd HH:mm:ss");
FileTab.Rows.Add(FileRow);
}

把目录中的文件属性读出来放到DATATABLE中,这回好排序了吧?
------解决方案--------------------
嗯。这个的确和环境有关系,GetFiles使用的是windows api函数FindFirstFile和FindNextFile,它们的对文件读取的顺序是不能修改的,按照windows内在的排序规则

规则参考
The order in which this function returns the file names is dependent on the file system type. With NTFS and CDFS file systems, the names are returned in alphabetical order. With FAT file systems, the names are returned in the order the files were written to the disk, which may or may not be in alphabetical order.

大致意思是
NTFS和CDFS下,是按照字母顺序,而FAT下,按照文件创建时间顺序