Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

Detecting open files in Delphi

Is there a way to detect if a file of any kind (for example .mp3-files) is opened? I want to know if a mp3-file is being played on the computer where my app is installed.
0
johnrt
Asked:
johnrt
1 Solution
 
Socrates050697Commented:
The only way I know to do this is to try opening the file with exclusive access. If the file is already open then it fails. Heres a function to do it -

note: this function will also return false if the file doesn't exist.

function isFileOpen(Filename: string): boolean;
var
  MyOfStruct: TOfStruct;
  FileHandle: integer;
begin
  FileHandle:=OpenFile(Filename,MyOfStruct,OF_SHARE_EXCLUSIVE);
  if FileHandle=HFILE_ERROR then
  begin
    result:=False;
  end
  else
  begin
    result:=True;
    CloseHandle(FileHandle);
  end;
end;

Hope this helps
Socrates
0
 
johnrtAuthor Commented:
Thank you. I'll try it...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now