6

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

    }

}

4

1 回答 1

2

查看这些文章:

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

这些可能有助于基于 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);
    }
}
于 2013-11-12T16:11:52.870 回答