我需要监视文件 (*.wav) 何时被执行。
我知道 filesystemwatcher 类。但是,当文件被使用时,此类会通知吗?启动 exe 时,我也遇到了Monitor。虽然我意识到这与 exe 有关,但有没有办法通知已执行 wav 文件?
问问题
107 次
1 回答
0
不,当非可执行文件(如 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; }
}
PS我知道这很丑:D
于 2017-01-30T06:54:45.870 回答