2009-01-06 61 views

回答

5

這是標籤爲Winapi,但我反正用.net反正它。

您可以使用System.IO中的FileSystemWatcher類進行此操作。

static void Main(string[] args) 
{ 
    FileSystemWatcher fsw = new FileSystemWatcher("C:\\"); 
    fsw.Changed += new FileSystemEventHandler(fsw_Changed); 
    fsw.Created += new FileSystemEventHandler(fsw_Created); 
    fsw.Deleted += new FileSystemEventHandler(fsw_Deleted); 
    fsw.EnableRaisingEvents = true; 
    fsw.IncludeSubdirectories = true; 
    fsw.Renamed += new RenamedEventHandler(fsw_Renamed); 

    Console.Read(); 
} 

static void fsw_Renamed(object sender, RenamedEventArgs e) 
{ 
    Console.WriteLine("{0} was changed to {1}", e.OldName, e.Name); 
} 

static void fsw_Deleted(object sender, FileSystemEventArgs e) 
{ 
    Console.WriteLine("{0} was deleted", e.Name); 
} 

static void fsw_Created(object sender, FileSystemEventArgs e) 
{ 
    Console.WriteLine("{0} was created", e.Name); 
} 

static void fsw_Changed(object sender, FileSystemEventArgs e) 
{ 
    Console.WriteLine("{0} was changed", e.Name); 
}