1

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.

3
  • what language are you using if you don't mind me asking?
    – user1931
    Oct 17, 2009 at 2:25
  • 1
    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. Oct 17, 2009 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, 2009 at 2:59

2 Answers 2

7

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>");
            Environment.Exit(0);
        }
        string path = args[0];
        try
        {
            File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None);
            Thread.Sleep(int.Parse(args[1]));
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine("File not found, exiting.");
            Environment.Exit(0);
        }
    }
}

Usage:

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 :)

4
  • Gosh, The Share parameter, of course! I think I need some sleep. Thanks John.
    – A Dwarf
    Oct 17, 2009 at 2:55
  • I was just trying to write something similar :( yours is much better than what I did! Oct 17, 2009 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, 2009 at 3:03
  • 1
    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
    – user1931
    Oct 17, 2009 at 6:13
1

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

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

And, as a one-liner:

(New-Object system.io.fileinfo("C:\Temp\tmp2F7F.tmp")).Open(3, 1, 0)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .