Is it possible to bypass a file lock in C# when another thread/process is unecessarily using an exclusive lock?

前端 未结 7 1037
半阙折子戏
半阙折子戏 2021-01-11 12:07

Is there a way to bypass or remove the file lock held by another thread without killing the thread?

I am using a third-party library in my app that is performing

7条回答
  •  生来不讨喜
    2021-01-11 12:57

    In short, you cannot do anything about the locking of the file by a third-party. You can get away with Richard E's answer above that mentions the utility Unlocker.

    Once the third-party opens a file and sets the lock on it, the underlying system will give that third-party a lock to ensure no other process can access it. There are two trains of thought on this.

    • Using DLL injection to patch up the code to explicitly set the lock or unset it. This can be dangerous as you would be messing with another process's stability, and possibly end up crashing the process and rendering grief. Think about it, the underlying system is keeping track of files opened by a process..DLL injection at the point and patch up the code - this requires technical knowledge to determine which process you want to inject into at run-time and alter the flags upon interception of the Win32 API call OpenFile(...).
    • Since this was tagged as .NET, why not disassemble the source of the third-party into .il files, and alter the flag for the lock to shared, rebuild the library by recompiling all .il files back together into a DLL. This of course, would require to root around the code where the opening of the file is taking place in some class somewhere.

    Have a look at the podcast here. And have a look here that explains how to do the second option highlighted above, here.

    Hope this helps, Best regards, Tom.

提交回复
热议问题