2011-11-02 55 views
0

目前我獲取文件名的文件的創建時間取決於起來......無法獲取文件名取決於文件的創建時間

我想是的,我想獲得的所有文件從02/10/2011至02/11/2011(今天的日期)。(30天差)

(IE)我想從(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( (File.GetCreationTime(Path.Combine(filenamepath,s)))。Year)== 0)select s))AddDays(-30))< 1)& &((DateTime.Now.Year - .ToList();

+0

乍一看,一切正常,但有一個原因你沒有使用則DateDiff,並可能簡化多個列表「s到一個列表? – bryanjonker

+0

我不知道如何在這種情況下使用thatiff ...你會請給這個任何示例片段.. – rockyashkumar

回答

1

行更改爲

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

UPDATE:

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(); 
+0

仍然我得到相同的錯誤.... – rockyashkumar

+0

嘗試更新... – Yahia

+0

我越來越錯誤像「例外」在選擇s「行..... – rockyashkumar

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();