2011-06-21 22 views
6

我正在用C#編寫一個小程序,該程序掃描一個文件夾並打開在程序按下按鈕後的5.30pm後創建的文件。這也必須在子文件夾內搜索。C#掃描一個文件夾並打開一段時間後創建的文件

我需要一些解決方案來指向正確的方向,因爲我不知道我會如何做到這一點。

這是一個文件夾觀察程序的一部分。問題是當用戶回家時PC關閉,並且在17.30之後有目錄創建文件。所以我需要一種方法,當程序在早上重新啓動時,它會檢測17.30之後創建的任何內容並打開它們。

private void button1_Click(object sender, EventArgs e) 
    { 
     folderBrowser.ShowDialog(); 

     textBox1.Text = folderBrowser.SelectedPath; 
     filewatcher.Path = textBox1.Text; 
     Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\COMPANY\\FOLDERWATCHER", "FOLDERPATH", textBox1.Text); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     String WatchFolder = Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\COMPANY\\FOLDERWATCHER", "FOLDERPATH", "").ToString(); 

     textBox1.Text = WatchFolder; 
     filewatcher.Path = WatchFolder; 
    } 

    private void Form1_Resize(object sender, EventArgs e) 
    { 
     if (WindowState == FormWindowState.Minimized) 
     { 
      ShowInTaskbar = true; 
      Hide(); 
     } 
    } 

    private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e) 
    { 
     if(!e.FullPath.EndsWith("temp.temp")) 
     { 
      MessageBox.Show("You have a Collection Form: " + e.Name); 
      Process.Start("explorer.exe", e.FullPath); 
     } 
    } 

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     Show(); 
    } 
} 

這是我上面的完整代碼。我想用一個按鈕打開或顯示17.30之後創建的文件。

回答

19

看看System.IO命名空間,它有你需要的一切。

the DirectoryInfo and 文件類將做你想做的。

+1

FileInfo/Directory.CreationTime更精確:http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.creationtime.aspx和http://msdn.microsoft.com/en-us /library/system.io.filesysteminfo.creationtime.aspx – RvdK

+2

@PoweRoy:好吧,我不想讓他變得很容易;) –

+3

所以SO是一個讓你爲你完成作業的地方。至少,當我看到其中一些問題時(單位代表的人),我會感受到這種感覺。 – dpurrington

3

您可以使用FileSystemWatcherMSDN documentation)檢測按下按鈕後(在應用程序運行時)創建的文件。

FileSystemWatcher watcher = new FileSystemWatcher(); 
watcher.Path = "C:\\YourDirectory"; 
watcher.Created += (sender, args) => { 
    // File was created 
} 
watcher.EnableRaisingEvents = true; 

這允許您在創建應用程序時(正在運行應用程序時)跟蹤文件。

如果您只想獲取在指定時間範圍內創建的所有目錄(應用程序啓動之前)的列表,則可以使用Directory.GetDirectoriesDirectory.GetFiles搜索目錄樹。

+3

這是獲得實時變化的正確方法,但他的問題聽起來像他需要在歷史上與實時進行比較。 – Nix

+0

嗨Directory.Getdirectories和Directory.GetFiles似乎是我正在尋找。 – Matt

1

代替日期時間地點你的日期和時間值。

void DirSearch(string dir) 
{ 
    try 
    { 
     foreach (string d in Directory.GetDirectories(dir)) 
     { 
      foreach (string f in Directory.GetFiles(d, "*.*")) 
      { 
       if(DateTime.Compare(f.GetCreationTime, datetime)) 
       { 
        //files found    
       } 
      } 
      DirSearch(d); 
     } 
    } 
    catch (System.Exception excpt) 
    { 
     Console.WriteLine(excpt.Message); 
    } 
} 
5

這裏是你正在尋找的遞歸方法:

public static List<string> GetFilesCreatedAfter(string directoryName, DateTime dt) 
{ 
    var directory = new DirectoryInfo(directoryName); 
    if (!directory.Exists) 
     throw new InvalidOperationException("Directory does not exist : " + directoryName); 
    var files = new List<string>(); 
    files.AddRange(directory.GetFiles().Where(n => n.CreationTime > dt).Select(n=>n.FullName)); 
    foreach (var subDirectory in Directory.GetDirectories(directoryName)) 
    { 
     files.AddRange(GetFilesCreatedAfter(subDirectory,dt)); 
    } 
    return files; 
} 

希望我幫助。

+0

謝謝你的信息,我怎麼能用一個按鈕來寫這個。 – Matt

+0

在您的按鈕的Click事件處理程序中,使用所需的directoryPath和DateTime調用此方法。即'var files = GetFilesCreatedAfter(folderBrowser。SelectedPath,新的DateTime(2011,06,24,17,30,0));' –

相關問題