.NET (PowerShell) Concurrent file usage (locking, while still allowing read access)

前端 未结 3 2189
面向向阳花
面向向阳花 2021-01-15 22:27

I have an XML file that I need to have concurrent access to. I do not need concurrent write, but I do need concurrent read. The perscribe

3条回答
  •  天涯浪人
    2021-01-15 22:56

    Maybe this can help. When you create a filestream there is an FileShare option. If you set it to ReadWrite, multiple processes can open that file

    $fsMain = [System.IO.File]::Open("C:\stack\out.txt", "Open", "ReadWrite", "ReadWrite")
    $fsReadOnly = [System.IO.File]::Open("C:\stack\out.txt", "Open", "Read", "ReadWrite")
    
    Write-Host ("fsMain:  CanRead=" + $fsMain.CanRead + ", CanWrite=" + $fsMain.CanWrite)
    Write-Host ("fsReadOnly:  CanRead= " + $fsReadOnly.CanRead + ", CanWrite=" + $fsReadOnly.CanWrite)
    
    $fsMain.Close()
    $fsReadOnly.Close()
    

提交回复
热议问题