0

Using c#, is there a way to check that a file is in use. More specifically, I need a way to monitor a directory of .wav files and see which sound file has been loaded. Note. It can be loaded by different applications.
I've had a look at Core Audio.. but I cannot seem to find an answer there. I've also tried to code that if the file is locked but that also doesnt provide a solution. So if you know of a way to determine which sound file (
.wav) is currently being played, please comment.

Eminem
  • 7,206
  • 15
  • 53
  • 95
  • so you want to see if file is opened by another application? or file is being used by another application and file is locked? or you want to see if .wav is actually begin played? I think these are whole different stories – M.kazem Akhgary Jan 30 '17 at 13:06
  • By in use I mean is the file being played. I thought that once a file is loaded that it would be locked.. but it isnt – Eminem Jan 30 '17 at 13:07
  • If the application loads the file into memory and then immediately closes it, then monitoring it via sampling will be error-prone to say the least. It will only be "in use" for the time it takes to load it into memory. – Matthew Watson Jan 30 '17 at 13:12
  • iOS or macOS? Sandboxed or not? The iOS and macOS sandboxes likely prevents any app from seeing whether another app has a file open for reading, or what audio they are playing. – hotpaw2 Jan 30 '17 at 18:45

1 Answers1

1
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;

try
{
    stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
    //the file is unavailable because it is:
    //still being written to
    //or being processed by another thread
    //or does not exist (has already been processed)
    return true;
}
finally
{
    if (stream != null)
        stream.Close();
}

//file is not locked
return false;
}

Here is the reference link : File is in Use

Hope it helps.

Community
  • 1
  • 1
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • whats the error? Could you post the error. This is a very generic solution that worked for almost all the scenarios. Thats why I have given you the link also. – Ranadip Dutta Jan 30 '17 at 13:11
  • 2
    If the file has been opened by the other process in `FileShare.Read` mode then this will return `false` even if the file is currently open. – Matthew Watson Jan 30 '17 at 13:14
  • Yes I know. However... (and you are going to *like* this). Im testing is on a machine that doesn't have a sound card. So what I do is I open up the file using notepad... because that shoud "lock" it right? i.e. an exception should get thrown from my app because notepad has the file open. But no exception is thrown – Eminem Jan 30 '17 at 13:15
  • Then I think you should use the [Extension Methods](https://msdn.microsoft.com/en-us/library/bb383977.aspx) – Ranadip Dutta Jan 30 '17 at 13:17
  • I Second @MatthewWatson. – Ranadip Dutta Jan 30 '17 at 13:18
  • @Eminem Notepad opens file in read mode, reads it and then closes it. Try it - you can open a file in Notepad, and delete it from Windows Explorer while still viewing it in Notepad. – Matthew Watson Jan 30 '17 at 13:20
  • @Matther Watson. Good point and noted. But now this still brings me to my point. How can I detect if a sound file is being played? Is there code to hook up into the audio system etc.? – Eminem Jan 30 '17 at 13:25
  • Because remember.. an audio player can do the same thing. Load the file into memory and close the file while having a copy of the data. Now when that data is being played.. I need to "catch" that – Eminem Jan 30 '17 at 13:26