刚才我没编译过,来个能编译通过的。public class FileInfoCount { public Int32 Count; public FileInfo FileInfo; public FileInfoCount(FileInfo fileInfo) { this.FileInfo = fileInfo; } }System.Collections.Generic.Dictionary<Int32, FileInfoCount> secondCountDic = new System.Collections.Generic.Dictionary<Int32, FileInfoCount>();public void Main() { System.Collections.Generic.IEnumerable<FileInfo> fileInfos = GetAllFileInfos("D:\\Test"); DateTime baseTime = new DateTime(2000, 1, 1); foreach (FileInfo fileInfo in fileInfos) { Int32 seconds = Convert.ToInt32((fileInfo.CreationTime - baseTime).TotalSeconds); AddTime(seconds, fileInfo); } foreach (System.Collections.Generic.KeyValuePair<Int32, FileInfoCount> key in secondCountDic) { if (key.Value.Count >= 5) continue; key.Value.FileInfo.CopyTo(""); } }private System.Collections.Generic.IEnumerable<FileInfo> GetAllFileInfos(string p) { throw new Exception("The method or operation is not implemented."); }private void AddTime(Int32 seconds, FileInfo fileInfo) { if (secondCountDic.ContainsKey(seconds)) secondCountDic.Add(seconds, new FileInfoCount(fileInfo)); else secondCountDic[seconds].Count++; }
如果遍历目录DirectoryInfo di = new DirectoryInfo(@"c:\"); var lessGroup = from fi in di.GetFiles("*.jpg") group fi by fi.CreationTime into g where g.Count() < 5 select g; //lessGroup就是不足5个的,考虑多组不足5个的情况 foreach (var g in lessGroup) { foreach (var item in g) { //item//就是FileInfo,你可以用这个来创建副本了 } }
group fi by fi.CreationTime不 过这个应该改成group fi by fi.CreationTime.TotalSeconds吧
需要循环来搞了
Dictionary<Int32,Int32> secondCountDic = new Dictionary<Int32,Int32>();public void Main()
{
IEnumerable<DateTime> crateTimes = GetAllFilesCreatedTimes();
DateTime baseTime = new DateTime(2000,1,1);
foreach(DateTime cratedTime in cratedTimes)
{
Int32 seconds = Convert.ToInt32((createdTime - baseTime).TotalSeconds);
AddTime(seconds);
} foreach(KeyValuePair<Int32,Int32> key in secondCountDic)
{
if(key.Value>=5) continue;
//
}
}private void AddTime(Int32 seconds)
{
if(secondCountDic .ContainsKey(seconds)) secondCountDic .Add(secons,1);
else secondCountDic [seconds]++;
}
public class FileInfoCount
{
public Int32 Count;
public FileFinfo FileInfo; public FileInfoCount(FileFinfo fileInfo){this.FileInfo = fileInfo;}
}Dictionary<Int32,FileInfoCount> secondCountDic = new Dictionary<Int32,FileInfoCount>();public void Main()
{
IEnumerable<FileInfo> fileInfos= GetAllFileInfos("D:\Test");
DateTime baseTime = new DateTime(2000,1,1);
foreach(FileInfo fileInfo in fileInfos)
{
Int32 seconds = Convert.ToInt32((createdTime - fileInfo.CreationTime).TotalSeconds);
AddTime(seconds,fileInfo);
} foreach(KeyValuePair<Int32,FileInfoCount> key in secondCountDic)
{
if(key.Value.Count>=5) continue;
key.Value.FileInfo.CopyTo(文件名);
}
}private void AddTime(Int32 seconds,FileInfo fileInfo)
{
if(secondCountDic .ContainsKey(seconds)) secondCountDic .Add(secons,new FileInfoCount(fileInfo));
else secondCountDic[seconds].Count++;
}
这个方法楼主自己能实现吧
我去试试哦。谢谢啦。
不要考虑遍历文件目录,因为文件目录的遍历会随着你文件数目增加而效率降低最终拖垮你的整个系统。用System.IO.FileSystemWatcher比较合适。
你给我的那段代码createdTime 和secons 没有定义??
{
public Int32 Count;
public FileInfo FileInfo; public FileInfoCount(FileInfo fileInfo) { this.FileInfo = fileInfo; }
}System.Collections.Generic.Dictionary<Int32, FileInfoCount> secondCountDic = new System.Collections.Generic.Dictionary<Int32, FileInfoCount>();public void Main()
{
System.Collections.Generic.IEnumerable<FileInfo> fileInfos = GetAllFileInfos("D:\\Test");
DateTime baseTime = new DateTime(2000, 1, 1);
foreach (FileInfo fileInfo in fileInfos)
{
Int32 seconds = Convert.ToInt32((fileInfo.CreationTime - baseTime).TotalSeconds);
AddTime(seconds, fileInfo);
} foreach (System.Collections.Generic.KeyValuePair<Int32, FileInfoCount> key in secondCountDic)
{
if (key.Value.Count >= 5) continue; key.Value.FileInfo.CopyTo("");
}
}private System.Collections.Generic.IEnumerable<FileInfo> GetAllFileInfos(string p)
{
throw new Exception("The method or operation is not implemented.");
}private void AddTime(Int32 seconds, FileInfo fileInfo)
{
if (secondCountDic.ContainsKey(seconds)) secondCountDic.Add(seconds, new FileInfoCount(fileInfo));
else secondCountDic[seconds].Count++;
}
var lessGroup = from fi in di.GetFiles("*.jpg") group fi by fi.CreationTime into g where g.Count() < 5 select g;
//lessGroup就是不足5个的,考虑多组不足5个的情况
foreach (var g in lessGroup)
{
foreach (var item in g)
{
//item//就是FileInfo,你可以用这个来创建副本了
}
}
我们不能用vs2008啊。。linq 也用不了啊。。郁闷