winapi - How to check if a file is open -
I need to check whether any other file in a folder is open by other applications. Unfortunately, if a file is open, GetFileAttributesA () returns the wrong value. Is there any remedy for this or am I doing something wrong?
GetFileAttributes
The way that somebody has opened the file (which can prevent you from opening it), it is to try to open itself.
bool , GENERIC_READ / * | GENERIC_WRITE is a part of /, 0, // 0-any null, open_real); If (hfile! = INVALID_HANDLE_VALUE) {CloseHandle (hfile); return false; } Return (GetLastError () == ERROR_SHARING_VIOLATION); }
But you do not like to write this function, because when you come to open th file for processing, you can open the file in any other application.
The only safe way to do this is to do what you want to do with the file, and when you try and open it for processing, note the error value if you fail Once you have the file open, you should keep it open until you can do it or some other process can open it behind your back (or delete it!).
Comments
Post a Comment