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

前端 未结 7 1086
半阙折子戏
半阙折子戏 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条回答
  •  梦毁少年i
    2021-01-11 12:53

    Any low level hack to do this may result in a thread crashing, file corruption or etc.

    Hence I thought I'd mention the next best thing, just wait your turn and poll until the file is not locked: https://stackoverflow.com/a/11060322/495455


    I dont think this 2nd advice will help but the closest thing (that I know of) would be DemandReadFileIO:

    IntSecurity.DemandReadFileIO(filename);
    
    internal static void DemandReadFileIO(string fileName) 
    {
       string full = fileName;            
       full = UnsafeGetFullPath(fileName); 
       new FileIOPermission(FileIOPermissionAccess.Read, full).Demand();
    }
    

提交回复
热议问题