问题1:
程序启动时,在指定目录C:\TXTDIR下,自动创建以时间为文件名的TXT文档,将到这个文档中记录信息,当TXT文档大小超过2M时,再自动以时间为文件名创建下一个TXT文档,以此类推。问题2:判断指定目录D:\fileDir下的文件数量,如果文件超过100个,删除最旧的文件,直到文件文件数量小于等于100为止。请给出这两个问题的代码,答出一个题给10分,两个题都给出代码给80分。回贴有分加!急,在线等,一有答案,马上结贴!

解决方案 »

  1.   

    if (!File.Exists(@"D:\fileDir\" +DateTime.Now.ToShortTimeString () ))
                    {
                        File.Create(@"D:\fileDir\" +DateTime.Now.ToShortTimeString () );
                    }
      

  2.   


    if(new FileInfo(fileName).Length>=2M)
    {
        File.Create(@"D:\fileDir\" +DateTime.Now.ToShortTimeString () );
    }
      

  3.   


     string[] files = Directory.GetFiles(path, "*.txt");            ArrayList fileList = new ArrayList(); ;
                if(files.Length >100)
                {
               
                    foreach (string file in files)
                    {
                        FileInfo info = new FileInfo(file);
                        fileList.Add(info);
                    }
                    //进行排序
                    ReverserClass reverser = new ReverserClass(typeof(FileInfo), "CreationTime", "DESC");
                    fileList.Sort(reverser);                while (fileList.Count > 100)
                    {
                        FileInfo info = (FileInfo)fileList[0];
                        info.Delete();
                        fileList.RemoveAt(0);
                    }
                }
      

  4.   

    dim a as system.io.directorydim go as bool=true
    while go
    dim b() as string=a.getfiles(path)
    if b.lenth >100 then
    dim c(b.lenth) as  datetime
    dim i as integer=0
    for each d as string in b
    c(i)=datetime.pase(d.replace(".txt",""))
    next
    array.sort(c)
    io.delete(c(0)& ".txt")
    if b.lenth =101 then
    go=false
    endif
    endif
      

  5.   


        private void button12_Click(object sender, EventArgs e)
        {
          Thread t = new Thread(new ThreadStart(Start));
          t.Start();
        }    private void Start()
        {
          while (true)
          {
            CreateFile();
          }
        }    private void CreateFile()
        {
          //文件命名不能用 用时间 ":"
          DateTime dt = DateTime.Now;
          string name = dt.ToShortDateString() +"-"+ dt.Millisecond.ToString()+ ".txt";
          string path = @"D:\aaa\" + name;
          if (File.Exists(path))
          {
            File.Delete(path);
          }
          //
          long size = 0;
          using (FileStream f = new FileStream(path, FileMode.CreateNew))
          {
            while (true)
            {
              byte[] b = System.Text.Encoding.UTF8.GetBytes("插入数据!插入数据!插入数据!插入数据!插入数据!插入数据!插入数据!插入数据!插入数据!插入数据!\r\n");
              f.Write(b, 0, b.Length);
              size += b.Length;
              if (size > 2 * 1024 * 1024)
              {
                break;
              }
            }
          }
        }
      

  6.   

    要去吃饭了,代码没敲完
    和lz说一下问题1的实现思路。
    string txtname=null;
    FileInfo txtLog=new FileInfo(@"C:\TXTDIR\" +txtname); //创建的txt文件
    int txtLength=1024;  //这里定义每个txt文件的大小,楼主说2M,自己改
    public void CreateLog(){                   //程序启动调用CreateLog()方法
       txtname=DateTime.Now.ToShortTimeString () .tosting(); //以时间命名文件
       File.Create(@"C:\TXTDIR\" +txtname);
    }public void WriteLog(string s)   //写个 WriteLog()方法,string s为要写入的日志类容
    {
      if(txtLog.length>txtLength){CreateLog();}//大于定义的文件大小就重新创建;
      else  //符合时就写入信息
     {
        System.IO.StreamWriter sw =new StreamWriter(txtLog.fullname,true);
        sw.Writeline(s);
        sw.close();
      }
    }在需要写入信息时调用WriteLog()方法,很早以前本人实现过...要吃饭了,楼主给分
      

  7.   

    问题2就是directoryInfo类中的getfileSystemInfos()遍历文件夹中的文件即可得解,实现办法lz自己动下脑筋.
    2题都已解决。80
      

  8.   

    saturn4263195
    先给你计10分,都回答完了才80,别耍赖!
      

  9.   

    public void Create(string name)
    {
     File.Create(@"D:\fileDir\" +name);
    }
    private int GetCount()
    {
     DirectoryInfo dir = new DirectoryInfo("");
     return dir.GetFiles().Length+1;
    }
    if (!File.Exists(@"D:\fileDir\" +DateTime.Now.ToShortTimeString () ))
    {
    Create(DateTime.Now.ToString("yyyy-MM-dd")+".txt")
    }
    else
    {
      FileInfo f = new FileInfo("");
      if(f.Length>2048)
       {Create(DateTime.Now.ToString("yyyy-MM-dd")+"_"+GetCount().ToString()+".txt")
    }
    } public void Del()
            {
          
                DirectoryInfo dir = new DirectoryInfo("");
               
                int i=dir.GetFiles().Length;
                if(i>100)         
               {
                foreach (FileInfo f in dir.GetFiles())
                {
                    
                }
                }
            }
      

  10.   

    先对文件进行时间排序,参考:http://topic.csdn.net/t/20041109/10/3534569.html
    后求出超出文件的数量,按下标删除
    directoryInfo dir= new directoryInfo(@"D:\fileDir");
    int count=0;// 遍历D:\fileDir中文件的数量;
    public int GetAllFiles(directoryInfo dir)//定义GetAllFiles()方法,传入参数;
    {
      FileSystemInfo[] fileinfo=dir.GetfileSystemInfos();
      foreach(FileSystemInfo i in fileinfo)
      {
          if(i is DirectoryInfo)  //如果D:\fileDir还存在文件夹,再次调用GetAllFiles()
          {
              GetAllFiles((DirectoryInfo)i);
          }      else{count++;}
       }
       return count;
    }
     
    前提是:先排序,在flie.delete  
      

  11.   

    囧,刚刚发现了一个可以GPS定位手机位置的网站,真不可思议!可以去看看
    http://hd.qu365.com/?P383126
      

  12.   

    class MyFileSortClass : IComparable
        {
            public DateTime TheValue;
            public string fileName;        public int CompareTo(object obj)             // Implement the method.
            {
                MyFileSortClass mc = (MyFileSortClass)obj;
                if (this.TheValue < mc.TheValue) return -1;
                if (this.TheValue > mc.TheValue) return 1;
                return 0;
            }        public MyFileSortClass(DateTime tD, string sf)
            {
                TheValue = tD;
                fileName = sf;
            }
        }
    //
                string[] sFiles = Directory.GetFiles(@"D:\fileDir");
                if (sFiles.Length > 100)
                {
                    ArrayList sFileInfo = new ArrayList();
                    foreach (string sFile in sFiles)
                    {
                        FileInfo f = new FileInfo(sFile);
                        MyFileSortClass msc = new MyFileSortClass(f.CreationTime, sFile);
                        sFileInfo.Add(msc);
                    }
                    //按照时间排序
                    sFileInfo.Sort();
                    //删除最前面的文件
                    int idel = sFileInfo.Count - 100;
                    for (int i = 0; i < idel; i++)
                    {
                        FileInfo f = new FileInfo(((MyFileSortClass)sFileInfo[i]).fileName);
                        f.Delete();
                    }
                }string[] sFiles = Directory.GetFiles(@"D:\fileDir", "*.txt");
                ArrayList sFileInfo = new ArrayList();
                foreach (string sFile in sFiles)
                {
                    FileInfo f = new FileInfo(sFile);
                    MyFileSortClass msc = new MyFileSortClass(f.CreationTime, sFile);
                    sFileInfo.Add(msc);
                }
                //按照时间排序
                sFileInfo.Sort();
                //检查最后文件的大小
                FileInfo f1 = null;
                if (sFileInfo.Count > 0)
                {
                    f1 = new FileInfo(((MyFileSortClass)sFileInfo[sFileInfo.Count - 1]).fileName);
                }
                if (f1 == null)
                {
                    //创建新文件
                    string txtname = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt";                File.Create(@"D:\fileDir\" + txtname);
                   
                }
                else
                {
                    if (f1.Length > 2000)
                    {
                        //创建新文件
                        string txtname = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt";  //以时间命名文件
                        File.Create(@"D:\fileDir\" + txtname);                }
                    else
                    {
                        //使用原来的文件
                    }
                }
      

  13.   


        class MyFileSortClass : IComparable
        {
            public DateTime TheValue;
            public string fileName;        public int CompareTo(object obj)             // Implement the method.
            {
                MyFileSortClass mc = (MyFileSortClass)obj;
                if (this.TheValue < mc.TheValue) return -1;
                if (this.TheValue > mc.TheValue) return 1;
                return 0;
            }        public MyFileSortClass(DateTime tD, string sf)
            {
                TheValue = tD;
                fileName = sf;
            }
        }
      

  14.   

    不行啊!用File.Create(@"D:\fileDir\" +name); 创建文件后,
    再向文件中写入数据时,提示:有一个进程正在使用该文件,不允许访问此文件啊!怎么办?请大家帮帮忙!
      

  15.   

    string txtname=null;
    FileInfo txtLog=new FileInfo(@"C:\TXTDIR\" +txtname); //创建的txt文件
    int txtLength=2048;  //这里定义每个txt文件的大小,楼主说2M,自己改
    public void CreateLog(){                   //程序启动调用CreateLog()方法
       txtname=DateTime.Now.ToShortTimeString () .tosting(); //以时间命名文件
       File.Create(@"C:\TXTDIR\" +txtname);
    }public void WriteLog(string s)   //写个 WriteLog()方法,string s为要写入的日志类容
    {
      if(txtLog.length>txtLength){CreateLog();}//大于定义的文件大小就重新创建;
      else  //符合时就写入信息
     {
        System.IO.StreamWriter sw =new StreamWriter(txtLog.fullname,true);
        sw.Writeline(s);
        sw.close();
      }
    }
      

  16.   

    第二题:      Dictionary<string, DateTime> dic = new Dictionary<string, DateTime>();
          string path = @"D:\fileDir";
          if (Directory.Exists(path))
          {
            Directory.CreateDirectory(path);
          }
          DirectoryInfo dirinfo = new DirectoryInfo(path);
          FileInfo[] fileinfo = dirinfo.GetFiles("*.txt");
          if (fileinfo.Length > 100) //Txt文件数量大于100
          {
            foreach (FileInfo file in fileinfo)
            {
              dic.Add(file.FullName, file.CreationTime); //添加到数据字典中
            }        //Linq 按时间排序
            IEnumerable<KeyValuePair<string, DateTime>> iedic = from ied in dic orderby ied.Value ascending select ied;
            int i = iedic.Count(); //得到集合个数
            foreach (var d in iedic)
            {
              File.Delete(d.Key);
              i--;
              if (i <= 100)
              {
                break;
              }
            }
          }//VS2008或以上版本 才可以运行
      

  17.   

          if (Directory.Exists(path))
          {
            Directory.CreateDirectory(path);
          }
    改成
          if (!Directory.Exists(path))
          {
            Directory.CreateDirectory(path);
          }