【问题标题】:Monitor when a file is in use监控文件何时被使用
【发布时间】:2017-01-30 05:45:21
【问题描述】:

我需要监视文件 (*.wav) 的执行时间。
我知道 filesystemwatcher 类。但是,当文件被使用时,此类会通知吗?我还遇到了Monitor when an exe is launched。虽然我意识到这与 exe 有关,但有没有办法通知已执行 wav 文件?

【问题讨论】:

  • 您可以在媒体播放器执行时进行跟踪。因为不能在 .docx 文件中写入文档。媒体播放器将执行.wav 格式。我认为这可以满足您的要求。如果没有,请在此处发表评论并缩小您的问题范围。

标签: c# wav file-monitoring


【解决方案1】:

不,当非可执行文件(如 mp3、wav、txt 等)打开时,您无法获得 Event。
但实际上现在可以检查文件是否打开写...

static void Main(string[] args)
{
    using (BackgroundWorker bw = new BackgroundWorker())
    {
        bw.DoWork += Bw_DoWork;
        bw.RunWorkerAsync();
    }
    Console.WriteLine("Press Q to exit");
    while (Console.ReadKey(true).Key!=ConsoleKey.Q){}
}

private static bool[] opened;

private static void Bw_DoWork(object sender, DoWorkEventArgs e)
{
    var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.mp3");
    opened = new bool[files.Length];

    while (true)
        for (int i = 0; i < files.Length; i++)
            try
            {
                using (var fs = File.Open(files[i], FileMode.Open, FileAccess.Read, FileShare.None))
                    opened[i] = false;
            }
            catch{ opened[i] = true; }
}

附:我知道这很丑:D

【讨论】:

  • 嘿,它可能很丑,但它可能是解决方案的一部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多