FileSystemWatcher 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 FileSystemWatcher 类的新实例。
重载
FileSystemWatcher() |
初始化 FileSystemWatcher 类的新实例。 |
FileSystemWatcher(String) |
在给定要监视的指定目录的情况下,初始化 FileSystemWatcher 类的新实例。 |
FileSystemWatcher(String, String) |
在给定要监视的指定目录和文件类型的情况下,初始化 FileSystemWatcher 类的新实例。 |
FileSystemWatcher()
- Source:
- FileSystemWatcher.cs
- Source:
- FileSystemWatcher.cs
- Source:
- FileSystemWatcher.cs
初始化 FileSystemWatcher 类的新实例。
public:
FileSystemWatcher();
public FileSystemWatcher ();
Public Sub New ()
示例
以下示例创建一个 FileSystemWatcher 对象,用于watch运行时指定的目录。 对象FileSystemWatcher监视和LastAccess
时间的变化LastWrite
,以及目录中文本文件的创建、删除或重命名。 如果文件已更改、创建或删除,则该文件的路径将显示在控制台中。 重命名文件时,控制台将显示新旧路径。
此示例使用 System.Diagnostics 和 System.IO 命名空间。
#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
注解
不能watch没有 Windows NT 或 Windows 2000 的远程计算机。 无法从 Windows NT 4.0 计算机watch远程 Windows NT 4.0 计算机。
下表显示了 实例 FileSystemWatcher的初始属性值。
属性 | 初始值 |
---|---|
NotifyFilter | 、FileName 、 和 的LastWrite 按位 OR 组合DirectoryName |
EnableRaisingEvents | false |
Filter | “*.*” (监视所有文件。) |
IncludeSubdirectories | false |
InternalBufferSize | 8192 |
Path | 空字符串 (“”) |
注意
在设置 并且 为 true
之前Path,EnableRaisingEvents组件不会watch指定的目录。
另请参阅
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
适用于
FileSystemWatcher(String)
- Source:
- FileSystemWatcher.cs
- Source:
- FileSystemWatcher.cs
- Source:
- FileSystemWatcher.cs
在给定要监视的指定目录的情况下,初始化 FileSystemWatcher 类的新实例。
public:
FileSystemWatcher(System::String ^ path);
public FileSystemWatcher (string path);
new System.IO.FileSystemWatcher : string -> System.IO.FileSystemWatcher
Public Sub New (path As String)
参数
- path
- String
要监视的目录,以标准表示法或通用命名约定 (UNC) 表示法表示。
例外
path
参数为 null
。
path
过长。
注解
注意
在设置 并且 为 true
之前Path,EnableRaisingEvents组件不会watch指定的目录。
该组件可以在个人计算机、网络驱动器或远程计算机上watch文件。
不能watch没有 Windows NT 或 Windows 2000 的远程计算机。 无法从 Windows NT 4.0 计算机watch远程 Windows NT 4.0 计算机。 默认情况下, 属性Filter设置为watch所有文件。
另请参阅
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
适用于
FileSystemWatcher(String, String)
- Source:
- FileSystemWatcher.cs
- Source:
- FileSystemWatcher.cs
- Source:
- FileSystemWatcher.cs
在给定要监视的指定目录和文件类型的情况下,初始化 FileSystemWatcher 类的新实例。
public:
FileSystemWatcher(System::String ^ path, System::String ^ filter);
public FileSystemWatcher (string path, string filter);
new System.IO.FileSystemWatcher : string * string -> System.IO.FileSystemWatcher
Public Sub New (path As String, filter As String)
参数
- path
- String
要监视的目录,以标准表示法或通用命名约定 (UNC) 表示法表示。
- filter
- String
要监视的文件的类型。 例如,“*.txt”监视所有文本文件的更改。
例外
path
过长。
注解
注意
在设置 并且 为 true
之前Path,EnableRaisingEvents组件不会watch指定的目录。
该组件可以在个人计算机、网络驱动器或远程计算机上watch文件。
不能watch没有 Windows NT 或 Windows 2000 的远程计算机。 无法从 Windows NT 4.0 计算机watch远程 Windows NT 4.0 计算机。
另请参阅
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes