Him*_*yal 5 c# clipboard filesystemwatcher
可以使用跟踪某些事件,例如文件,文件夹的创建,删除或重命名FileSystemWatcher,但是我也需要跟踪复制和粘贴。
我不想跟踪从.text .doc等文件复制的内容,我想跟踪整个文件或文件夹(例如d:\folder\movies复制到)的内容e:\movies。下面的代码示例将能够保留任何文件操作(如重命名,删除,创建,更改)的日志。Iam寻找复制/粘贴并移动文件。
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);
}
Run Code Online (Sandbox Code Playgroud)
}
查看这些文章:
FileSystemWatcher似乎是一个答案
直接来自文档
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这些可能有助于基于 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2451 次 |
| 最近记录: |