What is wrong if "the requested operation cannot be performed in a file with a section opened by the user?"

I am developing an application that reads and writes a lot (but synchronously) to a specific file on disk. However, the more the file gets, the more often I get the following IOException:

The requested operation cannot be performed in a file with a user-displayed section open.

comes from:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamWriter.CreateFile(String path, Boolean append) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding) at System.IO.File.WriteAllText(String path, String contents, Encoding encoding) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at TestApp.Program.Main() 

As you can see, the exception is thrown from File.WriteAllText . I suspected that this exception was due to both reading and writing, so I tried to restrict access to the file. Both a lock and a Mutex seemed to increase the number of exceptions rather than prevent them. So what is going on? What exactly does this exception exactly mean and how can it be prevented?

+6
c # exception file-io
source share
6 answers

I wrote to the file (open, loop (write), close), but after the error I duplicated the call in the routine ... so I ended up "The requested operation cannot be performed in the file with the open user section."! Well, I deleted the second call, and then the problem is resolved. It seems that two (open / closed) (open / close) routines over the same file that happen too quickly one after another make it ... Some developers suggest calling gc. Verify that each I / O operation is correctly closed. Do not complete (open, cycle (write), close) too early. It seems that when one operation is completed while the second request arrives, and this will cause a problem.

0
source share

The first solution is to disable your antivirus. Even better, add the folder to the scan exclusion list of your antivirus.

+6
source share

Do you have real-time authentication software?

I lost count of the number of errors associated with locking files that were caused by virus scanning software.

+1
source share

I saw this message when the Visual Studio debugger hits the node and locks the PDB file.

This may not be compatible with your situation, but you can confirm whether the file is locked (and what) by using a downloadable utility called "Unlocker" that can detect and remove file and folder locks.

+1
source share

Does this even happen with one thread / process?

If so, this is not a thread issue.

If not, you may not use the mutex / lock correctly. Maybe you should post the appropriate code here.

0
source share

For me, it looks like you have another process that opens all modified files with permissions, which means that you cannot open it for change. It can be an antivirus, online backup, synchronization of online files ...

0
source share

All Articles