Windows file opened by another process

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?

  1. Monitor the file
  2. Make a temp copy of it when it changes
  3. Read the temp copy
  4. 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.

Читайте также:  Linux add bom to file

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.

Читайте также:  Atom n2800 драйвер видеокарты windows 10

Content writer with a keen interest in global technology and pop culture trends.

How to check if a file is already open by another process in C?

I see that standard C has no way of telling if a file is already opened in another process. So the answer should contain several examples for each platform. I need that check for Visual C++ / Windows though.

8 Answers 8

There’s no way tell, unless the other process explicitly forbids access to the file. In MSVC, you’d do so with _fsopen() , specifying _SH_DENYRD for the shflag argument. The notion of being interested whether a file is opened that isn’t otherwise locked is deeply flawed on a multitasking operating system. It might be opened a microsecond after you’d have found it wasn’t. That’s also the reason that Windows doesn’t have a IsFileLocked() function.

If you need synchronized access to files, you’ll need to add this with a named mutex, use CreateMutex().

Windows: Try to open the file in exclusive mode. If it works, no one else has opened the file and will not be able to open the file

MS says: dwShareMode

The sharing mode of an object, which can be read, write, both, delete, all of these, or none (refer to the following table).

If this parameter is zero and CreateFile succeeds, the object cannot be shared and cannot be opened again until the handle is closed.

You cannot request a sharing mode that conflicts with the access mode that is specified in an open request that has an open handle, because that would result in the following sharing violation: ERROR_SHARING_VIOLATION.

extension: how to delete a (not readonly) file filesystem which no one has open for read/write?

access right FILE_READ_ATTRIBUTES, not DELETE. DELETE could cause problems on smb share (to MS Windows Servers) — CreateFile will leave with a still open FileHandle /Device/Mup:xxx filename — why ever and whatever this Mup is. Will not happen with access right FILE_READ_ATTRIBUTES use FILE_FLAG_OPEN_REPARSE_POINT to delete filename. Else you will delete the target of a symbolic link — which is usually not what you want

what to do with an open file? If the file is open and you are allowed to do an unlink, you will be left a file where subsequent opens will lead to ACCESS_DENIED. If you have a temporary folder, then it could be a good idea to rename(filename, tempdir/filename.delete) and delete tempdir/filename.delete.

Getting the open_files information is DIFFICULT, it’s like pulling teeth, and if you don’t have an immediate need for it you shouldn’t be asking for «several examples for each platform» just for the hell of it. Just my opinion, of course.

Linux and many Unix systems have a system utility called lsof which finds open file handles and stuff. The way it does so is by accessing /dev/kmem , which is a pseudo-file containing a copy of «live» kernel memory, i.e. the working storage of the operating system kernel. There are tables of open files in there, naturally, and the memory structure is open-source and documented, so it’s just a matter of a lot of busywork for lsof to go in there, find the information and format it for the user.

Documentation for the deep innards of Windows, on the other hand, is practically nonexistent, and I’m not aware that the data structures are somehow exposed to the outside. I’m no Windows expert, but unless the Windows API explicitly offers this kind of information it may simply not be available.

Whatever is available is probably being used by Mark Russinovich’s SysInternals utilities; the first one that comes to mind is FileMon. Looking at those may give you some clues. Update: I’ve just been informed that SysInternals Handles.exe is even closer to what you want.

Читайте также:  Error windows cannot connect to the printer

If you manage to figure that out, good; otherwise you may be interested in catching file open/close operations as they happen: The Windows API offers a generous handful of so-called Hooks: http://msdn.microsoft.com/en-us/library/ms997537.aspx. Hooks allow you to request notification when certain things happen in the system. I believe there’s one that will tell you when a program –systemwide– opens a file. So you can make your own list of files opened for the duration you’re listening to your hooks. I don’t know for sure but I suspect this may be what FileMon does.

The Windows API, including the hook functions, can be accessed from C. Systemwide hooks will require you to create a DLL to be loaded alongside your program.

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

Оцените статью