可以使用FileSystemWatcher
跟踪某些事件,例如创建,删除或重命名文件和文件夹,但我还需要跟踪复制和粘贴相同的内容。
我不想跟踪从.text .doc等文件中复制的内容,我想跟踪整个文件或文件夹,例如d:\folder\movies
被复制到e:\movies
。
下面是代码示例,它将能够保存任何文件操作的日志,如重命名,删除,创建,更改。我正在寻找复制/粘贴并移动alos。
class Program
{
static void Main(string[] args)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
ArrayList list = new ArrayList();
String s;
foreach (DriveInfo d in allDrives)
{
if (DriveType.Fixed == d.DriveType)
{
s = Convert.ToString(d);
list.Add(s);
Console.WriteLine(s.TrimEnd());
}
}
run(list);
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void run(ArrayList list)
{
ArrayList fw = new ArrayList();
FileSystemWatcher Clientwatcher = new FileSystemWatcher();
foreach (string s in list)
{
String temp=s;
temp=temp+'\\';
Clientwatcher.Path =temp;
Clientwatcher.Filter = "*.*";
Clientwatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
Clientwatcher.Changed += new FileSystemEventHandler(Clientwatcher_Changed);
Clientwatcher.Renamed += new RenamedEventHandler(Clientwatcher_Renamed);
Clientwatcher.Created += new FileSystemEventHandler(Clientwatcher_Created);
Clientwatcher.Deleted += new FileSystemEventHandler(Clientwatcher_Deleted);
Clientwatcher.EnableRaisingEvents = true;
fw.Add(Clientwatcher);
}
Console.WriteLine(fw.Capacity);
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
static void Clientwatcher_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
string s = e.FullPath + e.ChangeType;
File.AppendAllText("c:\\temp.txt", s);
}
static void Clientwatcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
string s = e.FullPath + e.ChangeType;
File.AppendAllText("c:\\temp.txt", s);
}
static void Clientwatcher_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType + e.OldFullPath);
string s = e.OldFullPath + " " + e.FullPath + " " + DateTime.Now + Environment.NewLine;
File.AppendAllText("c:\\temp.txt", s);
}
static void Clientwatcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
string s = e.FullPath + " " + e.ChangeType + " " + DateTime.Now + Environment.NewLine;
File.AppendAllText("c:\\temp.txt", s);
}
}
答案 0 :(得分:1)
查看这些文章:
FileSystemWatcher
似乎是一个答案
<强> LINK 强>
直接从文档
using System;
using System.IO;
using System.Security.Permissions;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// 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("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);
}
}
这些可能有助于基于WMI的方法。
http://www.codeproject.com/Articles/42212/WMI-and-File-System-Monitoring
http://msdn.microsoft.com/en-us/library/aa394594(v=vs.85).aspx
驱动器上所有文件夹中所有文件的递归实现:
using System;
using System.IO;
using System.Security.Permissions;
using System.Threading.Tasks;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
// Create a new FileSystemWatcher and set its properties.
RecurseDirectory();
Console.ReadKey();
}
private static void RecurseDirectory(string path = @"T:\")
{
try
{
SetWatcher(path);
Parallel.ForEach(Directory.GetDirectories(path), RecurseDirectory);
}
catch (Exception e)
{
//Ignore
}
}
private static void SetWatcher(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
//watcher.Filter = "*.txt";
// 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;
}
// 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);
}
}
答案 1 :(得分:0)