FileSystemWatcher.OnCreated(FileSystemEventArgs) FileSystemWatcher.OnCreated(FileSystemEventArgs) FileSystemWatcher.OnCreated(FileSystemEventArgs) FileSystemWatcher.OnCreated(FileSystemEventArgs) Method


引發 Created 事件。Raises the Created event.

 void OnCreated(System::IO::FileSystemEventArgs ^ e);
protected void OnCreated (System.IO.FileSystemEventArgs e);
member this.OnCreated : System.IO.FileSystemEventArgs -> unit
Protected Sub OnCreated (e As FileSystemEventArgs)



OnCreated在受監視的目錄中建立檔案或目錄時,會呼叫。OnCreated is called when a file or directory is created in the directory being monitored.

某些常見的情況,例如複製或移動檔案或目錄,並不會直接對應至事件,但這些情況會導致引發事件。Some common occurrences, such as copying or moving a file or directory, do not correspond directly to an event, but these occurrences do cause events to be raised. 當您複製檔案或目錄時,如果正在監看Created該目錄,系統會在複製檔案的目錄中引發事件。When you copy a file or directory, the system raises a Created event in the directory to which the file was copied, if that directory is being watched. 如果您複製的來原始目錄正由另一個實例FileSystemWatcher監看,則不會引發任何事件。If the directory from which you copied was being watched by another instance of FileSystemWatcher, no event would be raised. 例如,您會建立兩個實例FileSystemWatcherFor example, you create two instances of FileSystemWatcher. FileSystemWatcher1 會設定為監看「C:\My 檔」,並將 FileSystemWatcher2 設定為監看「C:\Your 檔」。FileSystemWatcher1 is set to watch "C:\My Documents", and FileSystemWatcher2 is set to watch "C:\Your Documents". 如果您從「我的文件」複製檔案,並將它貼入「您的檔」 Created ,則會在 FileSystemWatcher2 中引發事件,但不會針對 FileSystemWatcher1 引發任何事件。If you copy a file from "My Documents" and paste it into "Your Documents", a Created event will be raised in FileSystemWatcher2, but no event is raised for FileSystemWatcher1. 不同于複製,移動檔案或目錄會引發兩個事件。Unlike copying, moving a file or directory raises two events. 從上一個範例中,如果您將檔案從「我的文件」移至「您的檔Created 」,則會在 FileSystemWatcher2 中引發Deleted事件,並在 FileSystemWatcher1 中引發事件。From the previous example, if you moved a file from "My Documents" to "Your Documents", a Created event would be raised in FileSystemWatcher2 and a Deleted event would be raised in FileSystemWatcher1.

一旦建立檔案,就會引發事件。OnCreatedThe OnCreated event is raised as soon as a file is created. 如果檔案正在複製或傳輸到監看目錄中,則OnCreated會立即引發事件,後面接著一或多個OnChanged事件。If a file is being copied or transferred into a watched directory, the OnCreated event will be raised immediately, followed by one or more OnChanged events.

引發事件會透過委派叫用此事件處理常式。Raising an event invokes the event handler through a delegate. 如需詳細資訊, 請參閱處理和引發事件For more information, see Handling and Raising Events.


在衍生OnCreated(FileSystemEventArgs)類別中覆寫時,請務必呼叫基類的OnCreated(FileSystemEventArgs)方法。When overriding OnCreated(FileSystemEventArgs) in a derived class, be sure to call the base class's OnCreated(FileSystemEventArgs) method.