Search code examples
cwindows-xprenamefile-exists

Under Windows XP, with the C language: Checking if a file is open


In my application, written in C, under Windows-XP: how can I check if a file is already open by another application? One option is to rename the file, and to check if it was renamed. Another option is to open the file for appending. But these options are very time-consuming. Is there any other, less time-consuming solution to the problem?


Solution

  • Open a file in exclusive mode.

    HANDLE file = CreateFile(_T("MyFile"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
    if (file != INVALID_HANDLE_VALUE)
    {
        // file is not used by anyone else
        CloseHandle(file);
    }