如何获取 CD / DVD 驱动器上文件更改的通知?

How to get notifications for File Changes on CD / DVD drive?

我是 C# 的新手,必须在 C# 中开发 Windows 表单应用程序。此应用程序应跟踪以下内容。

我可以通过 RegisterNotification 和在 WndProc 方法中跟踪 WM_DEVICECHANGE 消息来获得 CD/DVD 驱动器插入的系统通知。

以上实现让我知道新设备何时连接到 PC。

我面临的问题是如何跟踪 CD/DVD(写入/修改)上发生的文件更改。一种选择是轮询 CD/DVD 中的文件作为后台作业。但是,这将是最后的选择。

我发现 IMAPI 我们可以通过它写入 CD/DVDs 但我只需要监视文件更改以进行审计。

请为我指出正确的方向,了解如何在我的程序中的 CD/DVD 通知中接收文件更改?

我试过 FileSystemWatcher 但它似乎不适用于 CD/DVD 驱动器。

2018 年 2 月 7 日更新:

我能找到的另一种方法是通过 WMI 附加到 WMI Events. I have found a question Best way to detect dvd insertion in drive c# which could also hold the answer. I wanted to know if the detection of DVD file system modification is feasible in WMI and if any experts can share the query for the same. I hope if Arshad 的查询将能够在这方面提供帮助。

方法 1:使用 FileSystemWatcher

public void ListOpticalDiscDrivesAndWatchRootDirectory()
{
    var drives = DriveInfo.GetDrives();

    foreach (var drive in drives)
    {
        if (drive.IsReady && drive.DriveType == DriveType.CDRom)
        {
            var rootDirectory = drive.RootDirectory.ToString();
            Console.WriteLine(rootDirectory);
            Watch(rootDirectory);
        }
    }
}

private void Watch(string path)
{
    var watcher = new FileSystemWatcher
    {
        Path = path,
        NotifyFilter = NotifyFilters.Attributes |
        NotifyFilters.CreationTime |
        NotifyFilters.DirectoryName |
        NotifyFilters.FileName |
        NotifyFilters.LastAccess |
        NotifyFilters.LastWrite |
        NotifyFilters.Security |
        NotifyFilters.Size,
        Filter = "*.*",
        EnableRaisingEvents = true
    };

    watcher.Changed += new FileSystemEventHandler(OnChanged);
}

private void OnChanged(object source, FileSystemEventArgs e)
{
    Console.WriteLine("Something changed!");
}

方法 2:使用 WMI

有一个 code project sample (VBScript) 描述了如何使用 WMI 进行文件系统监控。我在下面的 C# 片段中使用了该示例中的查询:

using System;
using System.Management;

public class OpticalDriveWatcher
{
    private ManagementEventWatcher _wmiWatcher = new ManagementEventWatcher();

    public ManagementEventWatcher WmiWatcher
    {
        get { return _wmiWatcher; }
    }

    private void OnWmiEventReceived(object sender, EventArrivedEventArgs e)
    {
        Console.WriteLine("WMI event!");
    }

    public void WatchWithWMI(string path)
    {
        string queryString = "Select * From __InstanceOperationEvent "
                           + "WITHIN 2 "
                           + "WHERE TargetInstance ISA 'CIM_DataFile' "
                           + $"And TargetInstance.Drive='{path}'";

        WqlEventQuery wmiQuery = new WqlEventQuery(queryString);
        WmiWatcher.Query = wmiQuery;
        WmiWatcher.Start();
    }
}

要注意的是 CIM_DataFile returns 只有本地固定磁盘上的文件实例。你可以这样称呼它

var detector = new OpticalDriveDetector();
var drive = "I:"; //You could get the optical drive you want to watch with DriveInfo as described in approach 1
detector.WatchWithWMI(drive);
detector.WmiWatcher.EventArrived += detector.OnWmiEventReceived;

当我用 DVD-RAM 测试时,这两种方法对我来说都很好。