Check Files in used
-
I am writing a program that will delete file(s) in a specified folder except file(s) in used. Please show me how to check whether a file is using or not. (Use C/C++ Non-MFC) Thanks a lot
Sincerely Thangnvhl
may be: HANDLE hFile = CreateFile("test.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,NULL); if(hFile == INVALID_HANDLE_VALUE){ if(GetLastError() == 5 || GetLastError() == 4) { //this file is being used } }else { CloseHandle(hFile); }
-
I am writing a program that will delete file(s) in a specified folder except file(s) in used. Please show me how to check whether a file is using or not. (Use C/C++ Non-MFC) Thanks a lot
Sincerely Thangnvhl
an alternative way i think is to use the OF_SHARE_EXCLUSIVE flag as the third argument for the OpenFile() function.
May all beings be happy and free...
-
may be: HANDLE hFile = CreateFile("test.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,NULL); if(hFile == INVALID_HANDLE_VALUE){ if(GetLastError() == 5 || GetLastError() == 4) { //this file is being used } }else { CloseHandle(hFile); }
aldo hexosa wrote:
if(GetLastError() == 5 || GetLastError() == 4)
Why use constants? It would be much more readable to use:
if (GetLastError() == ERROR_TOO_MANY_OPEN_FILES || GetLastError() == ERROR_ACCESS_DENIED)
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb