GetLastError() returs "File Already Exist" in all cases.
Main Topics
Browse All TopicsIs there any method/win32 call available to check if the file is in use by any other process.
I have a requirement where i need to check if the file is in use or not.
I found that i can try to open the file in a try/catch block and if any exception generates it means that file is being used by some other process. The problem is that this method is resource intensive as some of the files are huge in size and also it is not 100 percent perfect as exception may generate due to some other problems. I need something which has least resource requirement and very light weight and reliable.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
In such case even making a driver to enumerate open handles from kernel mode is not sufficient (because notepad closes the handle after reading).
I'd recommend handling each application individually, e.g. by enumerating windows and reading their titles. The notepad title says, for example, "Notepad - myfile.txt".
Business Accounts
Answer for Membership
by: GurudenisPosted on 2008-04-22 at 13:50:39ID: 21415465
Using CreateFile (similarly to the method you described) is reliable enough because you can call GetLastError() to see if the error was caused by sharing violation or by any other error. Also, CreateFile doesn't care about the size of the file, so it's not as resource intensive as it may seem. The same applies to CFile, though, because you can learn the reason for error by examining the CFileException object thrown. The only difference is that the exception mechanism is slightly less efficient in terms of runtime.