2

I need to monitor when a file (*.wav) is being executed.
I know about the filesystemwatcher class. However does this class notify when a file is being used? I also came across Monitor when an exe is launched. While I realise that this has to do with an exe, is there a way to be notified that a wav file has been executed?

Community
  • 1
  • 1
Eminem
  • 7,206
  • 15
  • 53
  • 95
  • You could trace whenever a media player is executed. Because a document can not be written within a .docx file. A media player will execute `.wav` format. I think this can fulfill your requirement. If not, comment here and narrow down your problem. – M. Adeel Khalid Jan 30 '17 at 06:35

1 Answers1

0

No, you can't get an Event when non executable files such as mp3, wav, txt and etc are opened.
But actually can check if file is opened write now...

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

P.S. I know it's ugly :D

Sergio
  • 346
  • 1
  • 8