比如说我会1秒钟往一个文件夹里生成5张图片,但是有时候1秒钟生成不够5张,我就想根据时间判断如果不够5张得话。就复制其中一张来填充。所以我想根据时间来分下组,因为有5张的时间是一样的。所以可以根据时间来判断哪秒 不够5张,怎么实现详细一点。有源码最好。谢啦

解决方案 »

  1.   

    这个要用 fileinfo 来获取文件创建时间了
    需要循环来搞了
      

  2.   


    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]++;
    }
      

  3.   


    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++;
    }
      

  4.   

    GetAllFileInfos(String path)
    这个方法楼主自己能实现吧
      

  5.   

    bclz_vs
     我去试试哦。谢谢啦。
      

  6.   

    还是用个定时器好了。1秒周期,搭配一个System.IO.FileSystemWatcher,将创建的文件添加列表,定时器中判断,不足5个就复制,反之清空列表等下一个周期。
    不要考虑遍历文件目录,因为文件目录的遍历会随着你文件数目增加而效率降低最终拖垮你的整个系统。用System.IO.FileSystemWatcher比较合适。
      

  7.   


    你给我的那段代码createdTime 和secons 没有定义?? 
      

  8.   

    刚才我没编译过,来个能编译通过的。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++;
    }
      

  9.   

    如果遍历目录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,你可以用这个来创建副本了
        }
    }
      

  10.   

    group fi by fi.CreationTime不 过这个应该改成group fi by fi.CreationTime.TotalSeconds吧
      

  11.   

    没实际测试过,没尝试1秒创建5张图片,效果一样吧。时间会做对比,TotalSeconds一致的,时间也应该是一样的。
      

  12.   


    我们不能用vs2008啊。。linq 也用不了啊。。郁闷