Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing a small tool and I need to test it again system-level locked files. I'm in search of some small application (or some easy method my tired brain is not coming up with) that allows me to lock and unlock files at will. Something like Unlocker but that allows me also to lock.

System is Windows. I expect anything that works under XP to work on the remaining OSes of this family. But for all that matters, the end target is Windows 7.

share|improve this question
what language are you using if you don't mind me asking? – John T Oct 17 '09 at 2:25
Not an answer but maybe help - I would ask on Stack Overflow for a code example on how to place an executive lock (I think it's called) on a file and simply create a very basic application around it, then run the program as system so you will be able to run it at will. I know of no application that exists that can already do this. – William Hilsum Oct 17 '09 at 2:26
The application is being written in C#. I guess I was just too tired and tried to walk away from writing my own lock/unlock tool (when preliminary attempts failed) and find instead some ready-made one. However given the nature of the answer that more than satisfies me, I'm voting for StackOverflow moving, if it's ok with you folks. – A Dwarf Oct 17 '09 at 2:59
up vote 7 down vote accepted

More StackOverflow geared, but I can't believe something this simple doesn't exist already. Here's the C# code (feel free to modify it, hacked it together quick):

using System;
using System.IO;
using System.Text;
using System.Threading;

class LockFile
    public static void Main(String[] args)
        if (args.Length > 2)
            Console.WriteLine("Usage: lockfile <file> <wait_in_ms>");
        string path = args[0];
            File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None);
        catch (FileNotFoundException e)
            Console.WriteLine("File not found, exiting.");


lockfile c:\somefile.txt 30000

locks the file specified for 30 seconds. When trying to access it you will a message like this:

alt text

Here's a compiled binary, .NET framework required. I'll recode it in another language if you need a native binary, just ask in the comments :)

share|improve this answer
Gosh, The Share parameter, of course! I think I need some sleep. Thanks John. – A Dwarf Oct 17 '09 at 2:55
I was just trying to write something similar :( yours is much better than what I did! – William Hilsum Oct 17 '09 at 2:56
I'm not using the sleep method as I plan to manually unlock them. And it also involves writing an explorer context menu for quicker access. So I'm not going to use this code at all. But thanks a bunch John. I can't explain how I missed the Share parameter. – A Dwarf Oct 17 '09 at 3:03
You're very welcome! You can take out the sleep switch and throw the sleep statement into an endless while loop if that makes things easier. I'm no .NET guru as you can probably see, this can be heavily improved upon. I sort of take stuff I know from other languages and piece it together using documentation when I don't know the language well :P – John T Oct 17 '09 at 6:13

Here is that same code in PowerShell (tested in PS v4, .NET 4.6, Win 8.1):

$file = New-Object"C:\Temp\tmp2F7F.tmp")
$stream = $f.Open([]::Open, []::Read, [System.IO.FileShare]::None)

And, as a one-liner:

(New-Object"C:\Temp\tmp2F7F.tmp")).Open(3, 1, 0)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.