Tôi đã truy cập trang này trong khi tìm kiếm cách theo dõi hoạt động của hệ thống tệp. Tôi lấy khúc xạ bài Paladin và FileSystemWatcher rằng ông đã chia sẻ và đã viết một tác C# thực hiện nhanh chóng-và-bẩn:
using System;
using System.IO;
namespace Folderwatch
{
class Program
{
static void Main(string[] args)
{
//Based on http://stackoverflow.com/questions/760904/how-can-i-monitor-a-windows-directory-for-changes/27512511#27512511
//and http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
string pathToFolder = string.Empty;
string filterPath = string.Empty;
const string USAGE = "USAGE: Folderwatch.exe PATH FILTER \n\n e.g.:\n\n Folderwatch.exe c:\\windows *.dll";
try
{
pathToFolder = args[0];
}
catch (Exception)
{
Console.WriteLine("Invalid path!");
Console.WriteLine(USAGE);
return;
}
try
{
filterPath = args[1];
}
catch (Exception)
{
Console.WriteLine("Invalid filter!");
Console.WriteLine(USAGE);
return;
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = pathToFolder;
watcher.Filter = filterPath;
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime |
NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Monitoring File System Activity on {0}.", pathToFolder);
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
}
Để sử dụng, tải về Visual Studio (Express sẽ làm). Tạo một ứng dụng giao diện điều khiển C# mới gọi là Folderwatch và sao chép và dán mã của tôi vào Program.cs của bạn.
Thay vào đó, bạn có thể sử dụng Sys Internals Process Monitor: Process Monitor Nó có thể giám sát hệ thống tệp và nhiều hơn nữa.
Có thể thực hiện với VBScript và WMI, xem việc triển khai này http://www.go-geek.com/tips-n-tricks/monitor-directory-for-new-files-with-wsh.html – Pradeep