Bagikan melalui


FileSystemWatcher.Created Kejadian

Definisi

Terjadi ketika file atau direktori dalam yang ditentukan Path dibuat.

public:
 event System::IO::FileSystemEventHandler ^ Created;
public event System.IO.FileSystemEventHandler? Created;
public event System.IO.FileSystemEventHandler Created;
[System.IO.IODescription("FSW_Created")]
public event System.IO.FileSystemEventHandler Created;
member this.Created : System.IO.FileSystemEventHandler 
[<System.IO.IODescription("FSW_Created")>]
member this.Created : System.IO.FileSystemEventHandler 
Public Custom Event Created As FileSystemEventHandler 

Jenis Acara

Atribut

Contoh

Contoh berikut menggunakan peristiwa untuk menampilkan jalur file ke konsol Created setiap kali file yang ditonton dibuat.

#include "pch.h"

using namespace System;
using namespace System::IO;

class MyClassCPP
{
public:

    int static Run()
    {
        FileSystemWatcher^ watcher = gcnew FileSystemWatcher("C:\\path\\to\\folder");

        watcher->NotifyFilter = static_cast<NotifyFilters>(NotifyFilters::Attributes
                                                         | NotifyFilters::CreationTime
                                                         | NotifyFilters::DirectoryName
                                                         | NotifyFilters::FileName
                                                         | NotifyFilters::LastAccess
                                                         | NotifyFilters::LastWrite
                                                         | NotifyFilters::Security
                                                         | NotifyFilters::Size);

        watcher->Changed += gcnew FileSystemEventHandler(MyClassCPP::OnChanged);
        watcher->Created += gcnew FileSystemEventHandler(MyClassCPP::OnCreated);
        watcher->Deleted += gcnew FileSystemEventHandler(MyClassCPP::OnDeleted);
        watcher->Renamed += gcnew RenamedEventHandler(MyClassCPP::OnRenamed);
        watcher->Error   += gcnew ErrorEventHandler(MyClassCPP::OnError);

        watcher->Filter = "*.txt";
        watcher->IncludeSubdirectories = true;
        watcher->EnableRaisingEvents = true;

        Console::WriteLine("Press enter to exit.");
        Console::ReadLine();

        return 0;
    }

private:

    static void OnChanged(Object^ sender, FileSystemEventArgs^ e)
    {
        if (e->ChangeType != WatcherChangeTypes::Changed)
        {
            return;
        }
        Console::WriteLine("Changed: {0}", e->FullPath);
    }

    static void OnCreated(Object^ sender, FileSystemEventArgs^ e)
    {
        Console::WriteLine("Created: {0}", e->FullPath);
    }

    static void OnDeleted(Object^ sender, FileSystemEventArgs^ e)
    {
        Console::WriteLine("Deleted: {0}", e->FullPath);
    }

    static void OnRenamed(Object^ sender, RenamedEventArgs^ e)
    {
        Console::WriteLine("Renamed:");
        Console::WriteLine("    Old: {0}", e->OldFullPath);
        Console::WriteLine("    New: {0}", e->FullPath);
    }

    static void OnError(Object^ sender, ErrorEventArgs^ e)
    {
        PrintException(e->GetException());
    }

    static void PrintException(Exception^ ex)
    {
        if (ex != nullptr)
        {
            Console::WriteLine("Message: {0}", ex->Message);
            Console::WriteLine("Stacktrace:");
            Console::WriteLine(ex->StackTrace);
            Console::WriteLine();
            PrintException(ex->InnerException);
        }
    }
};


int main()
{
    MyClassCPP::Run();
}
using System;
using System.IO;

namespace MyNamespace
{
    class MyClassCS
    {
        static void Main()
        {
            using var watcher = new FileSystemWatcher(@"C:\path\to\folder");

            watcher.NotifyFilter = NotifyFilters.Attributes
                                 | NotifyFilters.CreationTime
                                 | NotifyFilters.DirectoryName
                                 | NotifyFilters.FileName
                                 | NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.Security
                                 | NotifyFilters.Size;

            watcher.Changed += OnChanged;
            watcher.Created += OnCreated;
            watcher.Deleted += OnDeleted;
            watcher.Renamed += OnRenamed;
            watcher.Error += OnError;

            watcher.Filter = "*.txt";
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;

            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }

        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            Console.WriteLine($"Changed: {e.FullPath}");
        }

        private static void OnCreated(object sender, FileSystemEventArgs e)
        {
            string value = $"Created: {e.FullPath}";
            Console.WriteLine(value);
        }

        private static void OnDeleted(object sender, FileSystemEventArgs e) =>
            Console.WriteLine($"Deleted: {e.FullPath}");

        private static void OnRenamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine($"Renamed:");
            Console.WriteLine($"    Old: {e.OldFullPath}");
            Console.WriteLine($"    New: {e.FullPath}");
        }

        private static void OnError(object sender, ErrorEventArgs e) =>
            PrintException(e.GetException());

        private static void PrintException(Exception? ex)
        {
            if (ex != null)
            {
                Console.WriteLine($"Message: {ex.Message}");
                Console.WriteLine("Stacktrace:");
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine();
                PrintException(ex.InnerException);
            }
        }
    }
}
Imports System.IO

Namespace MyNamespace

    Class MyClassVB

        Shared Sub Main()
            Using watcher = New FileSystemWatcher("C:\path\to\folder")
                watcher.NotifyFilter = NotifyFilters.Attributes Or
                                       NotifyFilters.CreationTime Or
                                       NotifyFilters.DirectoryName Or
                                       NotifyFilters.FileName Or
                                       NotifyFilters.LastAccess Or
                                       NotifyFilters.LastWrite Or
                                       NotifyFilters.Security Or
                                       NotifyFilters.Size

                AddHandler watcher.Changed, AddressOf OnChanged
                AddHandler watcher.Created, AddressOf OnCreated
                AddHandler watcher.Deleted, AddressOf OnDeleted
                AddHandler watcher.Renamed, AddressOf OnRenamed
                AddHandler watcher.Error, AddressOf OnError

                watcher.Filter = "*.txt"
                watcher.IncludeSubdirectories = True
                watcher.EnableRaisingEvents = True

                Console.WriteLine("Press enter to exit.")
                Console.ReadLine()
            End Using
        End Sub

        Private Shared Sub OnChanged(sender As Object, e As FileSystemEventArgs)
            If e.ChangeType <> WatcherChangeTypes.Changed Then
                Return
            End If
            Console.WriteLine($"Changed: {e.FullPath}")
        End Sub

        Private Shared Sub OnCreated(sender As Object, e As FileSystemEventArgs)
            Dim value As String = $"Created: {e.FullPath}"
            Console.WriteLine(value)
        End Sub

        Private Shared Sub OnDeleted(sender As Object, e As FileSystemEventArgs)
            Console.WriteLine($"Deleted: {e.FullPath}")
        End Sub

        Private Shared Sub OnRenamed(sender As Object, e As RenamedEventArgs)
            Console.WriteLine($"Renamed:")
            Console.WriteLine($"    Old: {e.OldFullPath}")
            Console.WriteLine($"    New: {e.FullPath}")
        End Sub

        Private Shared Sub OnError(sender As Object, e As ErrorEventArgs)
            PrintException(e.GetException())
        End Sub

        Private Shared Sub PrintException(ex As Exception)
            If ex IsNot Nothing Then
                Console.WriteLine($"Message: {ex.Message}")
                Console.WriteLine("Stacktrace:")
                Console.WriteLine(ex.StackTrace)
                Console.WriteLine()
                PrintException(ex.InnerException)
            End If
        End Sub

    End Class

End Namespace

Keterangan

Beberapa kejadian umum, seperti menyalin atau memindahkan file atau direktori, tidak sesuai langsung dengan peristiwa, tetapi kejadian ini menyebabkan peristiwa dinaikkan. Saat Anda menyalin file atau direktori, sistem akan menaikkan Created peristiwa di direktori tempat file disalin, jika direktori tersebut sedang ditonton. Jika direktori tempat Anda menyalin sedang diawasi oleh instans lain , FileSystemWatchertidak ada peristiwa yang akan dinaikkan. Misalnya, Anda membuat dua instans FileSystemWatcher. FileSystemWatcher1 diatur ke watch "C:\My Documents", dan FileSystemWatcher2 diatur ke watch "C:\Your Documents". Jika Anda menyalin file dari "Dokumen Saya" ke "Dokumen Anda", Created peristiwa akan dinaikkan oleh FileSystemWatcher2, tetapi tidak ada peristiwa yang dinaikkan untuk FileSystemWatcher1. Tidak seperti menyalin, memindahkan file atau direktori akan menaikkan dua peristiwa. Dari contoh sebelumnya, jika Anda memindahkan file dari "Dokumen Saya" ke "Dokumen Anda", Created peristiwa akan dinaikkan oleh FileSystemWatcher2 dan Deleted peristiwa akan dinaikkan oleh FileSystemWatcher1.

Catatan

Operasi sistem file umum mungkin meningkatkan lebih dari satu peristiwa. Misalnya, ketika file dipindahkan dari satu direktori ke direktori lainnya, beberapa dan beberapa OnChangedOnCreated dan OnDeleted peristiwa mungkin dimunculkan. Memindahkan file adalah operasi kompleks yang terdiri dari beberapa operasi sederhana, oleh karena itu menaikkan beberapa peristiwa. Demikian juga, beberapa aplikasi (misalnya, perangkat lunak antivirus) dapat menyebabkan peristiwa sistem file tambahan yang terdeteksi oleh FileSystemWatcher.

Catatan

Urutan di mana Created peristiwa dinaikkan sehubungan dengan peristiwa lain FileSystemWatcher dapat berubah ketika SynchronizingObject properti bukan null.

Peristiwa OnCreated dimunculkan segera setelah file dibuat. Jika file sedang disalin atau ditransfer ke direktori yang diawasi, OnCreated acara akan segera dinaikkan, diikuti oleh satu atau beberapa OnChanged peristiwa.

Berlaku untuk

Lihat juga