Powershell script open textfile with executable and lock textfile

137 views Asked by At

I am trying to write a powershell script which open a textfile with an executable like notepad++ and lock the textfile on readmode, so nobody overwrite my changes while I'm working.

$lockFile = 'C:\new2.txt'

$FileStream = [System.IO.File]::Open($lockFile, 'Open', 'Write', 'Read')

Start-Process "C:\Program Files (x86)\Notepad++\notepad++.exe" $lockFile

The lock on the textfile is working fine, but I want to start notepad++ to work on that textfile, but it's already locked when I start Notepad.

Thanks in advance!

1

There are 1 answers

0
Alijvhr On

Actually it's not possible. when you lock a file it belongs to the process who opened it! You cannot pass it to other program.

Try this link. It might help.