C# 如何获取某文件夹的文件列表,并按文件的创建时间排序?
以ArrayList或数组返回,谢谢!1楼的朋友给的代码不能用,在发之前可以先测试一下吗?我是菜N,麻烦各位了,谢谢!...
以ArrayList或数组返回,谢谢!
1楼的朋友给的代码不能用,在发之前可以先测试一下吗?我是菜N,麻烦各位了,谢谢! 展开
1楼的朋友给的代码不能用,在发之前可以先测试一下吗?我是菜N,麻烦各位了,谢谢! 展开
展开全部
using System.Collections.Generic;
using System.IO;
List<FileInfo> files = new List<FileInfo>();
string[] fs=System.IO.Directory.GetFiles(@"****");
foreach(string file in fs)
{
files.Add(new FileInfo(file));
}
List<FileInfo> filesOrderedDESC = files.OrderByDescending(p => p.CreationTime).ToList();
List<FileInfo> filesOrderedASC = files.OrderBy(p => p.CreationTime).ToList();
using System.IO;
List<FileInfo> files = new List<FileInfo>();
string[] fs=System.IO.Directory.GetFiles(@"****");
foreach(string file in fs)
{
files.Add(new FileInfo(file));
}
List<FileInfo> filesOrderedDESC = files.OrderByDescending(p => p.CreationTime).ToList();
List<FileInfo> filesOrderedASC = files.OrderBy(p => p.CreationTime).ToList();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建立基类
public class FIleLastTimeComparer : IComparer<FileInfo>
{
int IComparer<FileInfo>.Compare(FileInfo x, FileInfo y)
{
return y.LastWriteTime.CompareTo(x.LastWriteTime);//递减
}
}
下面是引用
DirectoryInfo dirinfo = new DirectoryInfo(path+"." + extension);
FileInfo[] Files = dirinfo.GetFiles();
Array.Sort<FileInfo>(Files, new FIleLastTimeComparer());
if (!recursive)
{
if (Files.Length > 0)
return Files[0].FullName;
else
return null;
public class FIleLastTimeComparer : IComparer<FileInfo>
{
int IComparer<FileInfo>.Compare(FileInfo x, FileInfo y)
{
return y.LastWriteTime.CompareTo(x.LastWriteTime);//递减
}
}
下面是引用
DirectoryInfo dirinfo = new DirectoryInfo(path+"." + extension);
FileInfo[] Files = dirinfo.GetFiles();
Array.Sort<FileInfo>(Files, new FIleLastTimeComparer());
if (!recursive)
{
if (Files.Length > 0)
return Files[0].FullName;
else
return null;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class FileComparer : IComparer
{
public int IComparer.Compare (Object o1, Object o2)
{
FileInfo fi1 = o1 as FileInfo;
FileInfo fi2 = o2 as FileInfo;
return fi1.LastWriteTime.CompareTo(fi2.LastWriteTime);
}
}
public class MyClass
{
public FileInfo[] GetFiles(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
FileComparer fc = new FileComparer();
Array.Sort(files, fc);
return files;
}
}
{
public int IComparer.Compare (Object o1, Object o2)
{
FileInfo fi1 = o1 as FileInfo;
FileInfo fi2 = o2 as FileInfo;
return fi1.LastWriteTime.CompareTo(fi2.LastWriteTime);
}
}
public class MyClass
{
public FileInfo[] GetFiles(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
FileComparer fc = new FileComparer();
Array.Sort(files, fc);
return files;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class FileComparer : IComparer
{
int IComparer.Compare(Object o1, Object o2)
{
FileInfo fi1 = o1 as FileInfo;
FileInfo fi2 = o2 as FileInfo;
return fi1.LastWriteTime.CompareTo(fi2.LastWriteTime);
}
}
public class MyClass
{
public FileInfo[] GetFiles(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
FileComparer fc = new FileComparer();
Array.Sort(files, fc);
return files;
}
}
{
int IComparer.Compare(Object o1, Object o2)
{
FileInfo fi1 = o1 as FileInfo;
FileInfo fi2 = o2 as FileInfo;
return fi1.LastWriteTime.CompareTo(fi2.LastWriteTime);
}
}
public class MyClass
{
public FileInfo[] GetFiles(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
FileComparer fc = new FileComparer();
Array.Sort(files, fc);
return files;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
引入命名空间
using System.IO;
using System.Collections;
public class FileComparer : IComparer
{
int IComparer.Compare(Object o1, Object o2)
{
FileInfo fi1 = o1 as FileInfo;
FileInfo fi2 = o2 as FileInfo;
return fi1.CreationTime.CompareTo(fi2.CreationTime);
}
}
public class MyClass
{
public FileInfo[] GetFiles(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
FileComparer fc = new FileComparer();
Array.Sort(files, fc);
return files;
}
}
使用
FileInfo[] a = new MyClass().GetFiles(@"D:\data");
using System.IO;
using System.Collections;
public class FileComparer : IComparer
{
int IComparer.Compare(Object o1, Object o2)
{
FileInfo fi1 = o1 as FileInfo;
FileInfo fi2 = o2 as FileInfo;
return fi1.CreationTime.CompareTo(fi2.CreationTime);
}
}
public class MyClass
{
public FileInfo[] GetFiles(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
FileComparer fc = new FileComparer();
Array.Sort(files, fc);
return files;
}
}
使用
FileInfo[] a = new MyClass().GetFiles(@"D:\data");
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询