How to purposefully exclusively lock a file?

Simpler solution: In the directory of interest run from cmd line:

notepad >filetolock

As re-directed stdout, it will remain locked until the application (notepad) is terminated.

Note that the "filetolock" will be overwritten by the re-direct, so you probably don't want to use an existing file of any importance. An empty "filetolock" won't matter to the application you're trying to test, as the app won't be able to open it anyway.


Lock a file without 3rd party tools, no changes to the file being locked and file can't even be copied

This PowerShell script is a quote from an answer to a similar question.

If you find it helpful, you may upvote the original answer and not this posting.

#Specify the file name
$fileName = "C:\myfile.txt"

#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()

Open it with MS-Excel... this app locks a file while open.