c# - 如何跟踪文件和文件夹的复制/粘贴事件

标签 c# clipboard filesystemwatcher

可以使用 FileSystemWatcher 跟踪某些事件,例如文件和文件夹的创建、删除或重命名。 ,但我还需要跟踪复制和粘贴。

我不想跟踪从 .text .doc 等文件中复制的内容,我想跟踪整个文件或文件夹,例如 d:\folder\movies复制到 e:\movies .
下面是代码示例,它将能够保留任何文件操作的日志,如重命名、删除、创建、更改。我正在寻找复制/粘贴和移动。

 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);

    }

}

最佳答案

查看这些文章:
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

http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/



驱动器上所有文件夹中所有文件的递归实现:
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);
    }
}

关于c# - 如何跟踪文件和文件夹的复制/粘贴事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19934133/

相关文章:

javascript - ASP.Net Core 3 和简单的 POST Controller

c# - 将关键字等效项从 Java 更新为 C#

javascript - 使用 Clipboard.js 复制跨度文本

winforms - 带有 FileSystemWatcher 的 Windows 窗体未启动子窗体

c# - FileSystemWatcher 超越文件系统权限

c# - 当泛型类型匹配采用该类型的重载方法时,Visual Studio 2015 不编译

c# - 只运行一个应用程序实例,(退出新实例并显示旧实例或给予焦点)

python - 在 Blender (python) 中从 Windows 剪贴板读取二进制数据

ios - 使标签可复制

c# - 为什么 FileSystemWatcher 检测不到来自 Visual Studio 的更改?