Skip to content

Instantly share code, notes, and snippets.

@rweijnen
rweijnen / KeepFileInUse.ps1
Created March 27, 2023 14:11
PowerShell example how you can keep a file in use, denying read/write access to other processes
# create test.txt in temp folder
$filename = Join-Path -Path $env:temp -ChildPath 'test.txt'
# make sure the file exists
"" | Out-File -LiteralPath $filename
# Open the file with Share options only allowing read...
$f = [System.IO.File]::Open($filename, 'Open', 'Read', 'Read')
"File $fileName is in use and locked"
@rweijnen
rweijnen / test.pas
Last active December 1, 2021 12:36
Enumerate handles example
function GetObjectName(const hObject: THandle): String;
var
oni: OBJECT_NAME_INFORMATION;
cbSize: DWORD;
nts: NTSTATUS;
begin
Result := '';
cbSize := SizeOf(oni) - (2 * SizeOf(USHORT));