可以使用 跟踪某些事件,例如文件和文件夹的创建、删除或重命名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);
}
}