无法获取文件名取决于文件创建时间

时间:2011-11-02 14:51:11

标签: c# .net winforms file

目前我正在检索文件名取决于文件的创建时间......

我想要的是,我想从2011年10月2日到2011年11月2日(今天的日期)获取所有文件。(30天差异)

(即)我想从(today day - 30 days

获取文件名

我希望从(today day -90 days

获取文件名

因为我已经这样做了....

private void filteringFiles()
{
  string filenamepath = @"C:\defualt\Access\backupdb\";
  List<String> listfiles = Directory.GetFiles(@"C:\defualt\Access\backupdb\", "backup-*.zip").ToList();
  List<String> files = new List<String>();
  List<String> getfiles = new List<String>();
  foreach (var allfiles in listfiles)
  {

    files.Add(Path.GetFileName(allfiles));
    //DateTime creationtime = File.GetCreationTime(files);
  }
  if (cbbackupforms.Text == "Month")
  {
    getfiles = (from string s in files where (DateTime.Now.Day - Convert.ToInt32(File.GetCreationTime(Path.Combine(filenamepath, s)).AddDays(-30)) < 1) && (DateTime.Now.Year - File.GetCreationTime(Path.Combine(filenamepath, s)).Year == 0) select s).ToList();

  }
  if (cbbackupforms.Text == "3 Months")
  {

    getfiles = (from string s in files where (DateTime.Now.Day - Convert.ToInt32(File.GetCreationTime(Path.Combine(filenamepath, s)).AddDays(-90)) < 3) && (DateTime.Now.Year - File.GetCreationTime(Path.Combine(filenamepath, s)).Year == 0) select s).ToList();

  }    

  if (cbbackupforms.Text =="6 Months")
  {
    getfiles = (from string s in files where (DateTime.Now.Day - Convert.ToInt32(File.GetCreationTime(Path.Combine(filenamepath, s)).AddDays(-180)) < 6) && (DateTime.Now.Year - File.GetCreationTime(Path.Combine(filenamepath, s)).Year == 0) select s).ToList();

  }
  if (cbbackupforms.Text == "year")
  {
    getfiles = (from string s in files where ((DateTime.Now.Month - File.GetCreationTime(Path.Combine(filenamepath, s)).Month) < 12) select s).ToList();

  } 


  listbackups.DataSource = getfiles;

}

但是我在这一行得到了错误

(DateTime.Now.Year - File.GetCreationTime(Path.Combine(filenamepath, s)).Year == 0) select s) 
像这样......

ERROR:{"Invalid cast from 'DateTime' to 'Int32'."}

任何人都会对此有所帮助...... 非常感谢...

更新:我仍然得到像这样的错误......

ERROR:{"Invalid cast from 'DateTime' to 'Int32'."}

在这一行

getfiles =(来自文件中的字符串s 其中(DateTime.Now.Day - Convert.ToInt32(File.GetCreationTime(Path.Combine(filenamepath,s))。AddDays(-30))&lt; 1) &amp;&amp;((DateTime.Now.Year - (File.GetCreationTime(Path.Combine(filenamepath,s)))。Year)== 0)select s)。ToList();

2 个答案:

答案 0 :(得分:1)

将行更改为

((DateTime.Now.Year - (File.GetCreationTime(Path.Combine(filenamepath, s))).Year) == 0) select s)

更新:

getfiles = (from string s in files where (DateTime.Now.Day - Convert.ToInt32(File.GetCreationTime(Path.Combine(filenamepath, s)).AddDays(-30).Day) < 1) && ((DateTime.Now.Year - (File.GetCreationTime(Path.Combine(filenamepath, s))).Year) == 0) select s).ToList();

答案 1 :(得分:0)

示例代码:我在想VB的东西。但是,这应该做你需要的吗?

        string filenamepath = @"C:\";   
        List<String> files = new List<String>();

        //change this TimeSpan based on your variable? 
        var timespan = new TimeSpan(30, 0,0,0,0);
        var dir = new DirectoryInfo(filenamepath);
        files = dir.GetFiles().Where(f => f.CreationTime.Add(timespan) > DateTime.Now).Select(f => f.Name).ToList();