- HOWTO: Monitoring file access by another process in Windows?
- Reading a file used by another process [duplicate]
- 2 Answers 2
- How to Solve “File Is Open in Another Program” Error in Windows 10
- Restart your computer
- Locate the File in Task Manager
- Restart file explorer through task manager
- Turn off the caching of thumbnails in hidden thumbs.db files
- Delete temporary files
- Empty Recycle bin
- Check the folder for viruses
- Conclusion
- On Windows, how to open for writing a file already opened for writing by another process?
- 2 Answers 2
- File used by another process [duplicate]
- 6 Answers 6
- Not the answer you’re looking for? Browse other questions tagged c# file-io or ask your own question.
- Linked
- Related
- Hot Network Questions
HOWTO: Monitoring file access by another process in Windows?
I am working on a program that processes image files. The problem I am running into is some of the images are very large and I don’t know when the copy process is done. Right now the code is written in C# and is using the FileSystemWatcher class to monitor the files. The only problem is there is no event for when another process is DONE with the file.
The current logic is to watch for created and changed events and than wait 5 seconds in hopes that by that point in time the copy process is done. While this works on my development machine, unfortunately customers report issues with the whole process.
Years ago when I wrote this code I did see that at a lower level, I think it was part of the DDK SDK or something, it is possible to find out a lot more information about how another program is accessing a file.
Can anyone speak into this and point me in the right direction? I do expect it to be C++ code, not C# code.
To answer some questions below:
The issue is that OTHER processes are copy images into directories that my program is monitoring. Once the images are fully copied my program needs to start processing the images as soon as possible. The issue is that many images, at times hundreds of images, will be copied at one time. Since each image needs to be processed as fast as possible, the system needs to know as soon as possible that the image is fully copied and can be processed.
I am fully aware there is NO way to do what I want to do via the WinSDK, but I am 99.9% sure it IS possible via the WinDDK, maybe I have the name wrong, I Googled that and got «Windows Driver Kit (WDK)». I am pretty sure the way SysInternals pulls off all their cool tricks is by using WDK level functions.
Reading a file used by another process [duplicate]
I am monitoring a text file that is being written to by a server program. Every time the file is changed the content will be outputted to a window in my program.
The problem is that I can’t use the Streamreader on the file as it is being used by another process . Setting up a Filestream with ReadWrite won’t do any good since I cannot control the process that is using the file.
I can open the file in notepad. It must be possible to access it even though the server is using it.
Is there a good way around this?
Should I do the following?
- Monitor the file
- Make a temp copy of it when it changes
- Read the temp copy
- Delete the temp copy.
I need to get the text in the file whenever the server changes it.
2 Answers 2
If notepad can read the file then so can you, clearly the program didn’t put a read lock on the file. The problem you’re running into is that StreamReader will open the file with FileShare.Read. Which denies write access. That can’t work, the other program already gained write access.
You’ll need to create the StreamReader like this:
Guessing at the Encoding here. You have to be careful with this kind of code, the other program is actively writing to the file. You won’t get a very reliable end-of-file indication, getting a partial last line is quite possible. In particular troublesome when you keep reading the file to try to get whatever the program appended.
How to Solve “File Is Open in Another Program” Error in Windows 10
While using Windows 10, you may have tried to delete a file or move it to another location, and encountered an error which states “Action Cannot Be Completed Because The File is Open in Another Program”, even though you have not opened the file in any program. There are a number of reasons why this error might arise, so here are ways to tackle each of those reasons and solve the error:
Restart your computer
Turn off your computer and then start it up again. This method will end all pending tasks that may be causing the error and allow your File Explorer to restart its internal processes from scratch, which might help solve the issue you’re having. If not, move on to the next step.
Locate the File in Task Manager
Use the shortcut Ctrl + Shift + Esc to open Task Manager.
Look for the file in the Processes tab, which shows you all the applications that are currently being used, whether with or without your knowledge.
Select the file and tap on the “End Task” option near the bottom of the Manager window to stop the file from being used by a program.
Go back to the file and try to delete it again. If it still doesn’t work move on to the next step.
Restart file explorer through task manager
Once again, open Task Manager and go to the Processes tab.
Here you will find the Windows Explorer program currently in use. Select the process and tap on the “End Task” option.
Go to File at the top of the Manager window and select “Run New Task”.
In the new window that opens type “explorer.exe” and hit Enter.
This process will reboot Windows Explorer to clear up any problems with its memory or cached files that might have been causing the issue.
Once again go to the file and try to delete it. If it still doesn’t work move on to the next step.
Turn off the caching of thumbnails in hidden thumbs.db files
Microsoft has admitted in the past that the thumbnails cache relating to files and folders can cause the “File is Open In Another Program” error, so tackling the thumbnails cache can help resolve the error.
2. Type gpedit.msc and then hit Enter.
3. In the new window that pops up, go to “User Configuration -> Administrative Templates -> Windows Components -> File Explorer.”
4. Go to the right pane and double-click on the “Turn off the caching of thumbnails in hidden thumbs.db files.”
5. Select the button next to Enabled and then Apply, followed by OK.
All the thumbnails in File Explorer will now be disabled, allowing you to carry out the desired action on the file. You can then re-enable the thumbnails by following the above steps and changing the policy back to Not Configured.
Delete temporary files
Temporary files are stored on your computer every time you modify a file in any manner. These temporary files many be preventing you from moving or deleting the related files. Here is how you can get rid of the temporary files from your computer storage.
2. Type %temp% into the input bar and press Enter.
3. In the new folder that opens containing all the temporary files stored in your File Explorer, press Ctrl + A to select all the files and delete them together.
4. There may still be temporary files stored in another location. Again, press Win + R , type temp and press Enter.
5. Once again, select all the temporary files in the folder that opens and delete them all.
Empty Recycle bin
Sometimes, sending a file to the Recycle bin can be viewed as an action by your computer, which results in the error we have been trying to solve. Another way to solve the issue is to go to your Recycle bin and empty the entire folder by selecting the files and deleting them. Be warned that this will delete all the files in the recycle bin permanently, so make sure to only select the files you are absolutely sure you will no longer need.
Check the folder for viruses
Finally, if none of the above methods work, the problem might be more serious. There might be a virus associated with the file that is preventing you from making changes to the file. Run the file through your antivirus software to locate any virus or malware it might contain.
If this does turn out to be the case, isolate the file using your antivirus software, and delete it before it can infect other files.
Conclusion
The “File Is Open in Another Program” error can be a frustrating problem to encounter. Fortunately, there are a number of ways to tackle the issue as mentioned in the previous sections. If the cause of the issue turns out to be a virus, it is better to be aware of the fact that there is a virus inside your computer so you can take steps to remove the malware from your device.
Content writer with a keen interest in global technology and pop culture trends.
On Windows, how to open for writing a file already opened for writing by another process?
I’m trying to open a logfile which is kept open by another process and remove the first few lines. On Unix I’d simply do a os.open(‘/tmp/file.log’, os.O_NONBLOCK) and that would get me closer to my goal.
Now i’m stuck with Windows and I need to rotate this log somehow without ending the application holding the file. Is this even possible?
At first I considered opening a file handle on the location where the application expected the log to be and just act as a pipe into a file-handle in Python but I couldn’t find any way of doing that either on Windows.
I also thought of just moving the file on a regular basis and letting the application recreate the file but since it’s being used by another process that doesn’t do much good.
Thought of O_SHLOCK as well but then again, that’s Unix and not Windows. So I went for mmap the file and hope that it would make it a bit more flexible but that led me nowhere.
This results in that the application can’t access the file because Python is holding it (and vice versa).
Came to think of signal.SIGHUP but that doesn’t exist in Windows either so back to square one.
I’m stuck and I’ve tried it all, can Python help me here or do I need to switch my language?
2 Answers 2
Even if the application opens the file as a shared object Python can’t so they can’t get along by the looks of it.
It’s not so bad :). You can (have to) open a file using CreateFile as pointed out by Augusto. You can use standard ctypes module for this. In the question Using a struct as a function argument with the python ctypes module you can see how to do it. Then you have to associate a C run-time file descriptor with an existing operating-system file handle you obtained in the previous step. You can use _open_osfhandle from the MS C run-time library (CRT) to do this. You can call it once again using ctypes; you can access it as ctypes.cdll.msvcrt._open_osfhandle . Then you have to associate Python file object with an existing C run-time file descriptor you obtained in the previous step. To do this in Python 3 you simply pass file descriptor as the first argument to the built-in open function. According to docs
file is either a string or bytes object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped.
In Python 2 you have to use os.fdopen ; its task, according to docs, is to
Return an open file object connected to the file descriptor fd
File used by another process [duplicate]
Many a times we get an error, while trying to write file on Windows platform,
«The process cannot access the file ‘XXX’ because it is being used by another process.»
How to check in C#, before writing to file that its not being used by another process?
6 Answers 6
You can’t, basically — you have to attempt to open the file for writing, and if you get an exception you can’t write to it 🙁
Even if you could do a separate check first, the result would be out of date before you could start writing — you could still end up with the exception later.
It would be nice if the framework provided a TryOpen method, admittedly.
The only option here is to catch the generated exception and handle it appropriately. See these other two SO threads for full discussions:
You must to close your file after using it on your code:
You simply have to try the open with the sharing access you would like, and handle the error that is thrown.
Note: Sometimes the file won’t open simply because the file sharing rights you specified conflicts with the file sharing rights that someone already opened the file with.
Try putting the open command in a try-catch statement. If the file is being used IOException will be thrown.
Try to Rename the file if you are able to rename then it is confirm that no other process is accessing that file. Finally make sure that you have renamed the same file in the same folder.
Not the answer you’re looking for? Browse other questions tagged c# file-io or ask your own question.
Linked
Related
Hot Network Questions
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.