Skip to content

Instantly share code, notes, and snippets.

@jimbobbennett
Created July 2, 2015 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimbobbennett/617d5741dcbf09a01848 to your computer and use it in GitHub Desktop.
Save jimbobbennett/617d5741dcbf09a01848 to your computer and use it in GitHub Desktop.
Code to put an exclusive lock on a file
param([string]$fileName)
#Open the file in read only mode, without sharing (I.e., locked as requested)
$file = [System.io.File]::Open($fileName, 'Open', 'Read', 'None')
#Wait in the above (file locked) state until the user presses a key
Write-Host "Press any key to continue ..."
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
#Close the file
$file.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment