c# FileLock – If you need to lock a file for temporary purpose.

Hi Everyone ,

FileLock is vey simple tool that locking defined file in given path. This tool can be used acting as AV programs , troublesoothing or reproduce error when a file is locked scenarios .
You can download binaries or source from :

http://spstools.codeplex.com/releases/view/80978

FileLock.cs
  1. class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          if (args.Length != 1)
  6.          {
  7.              Console.WriteLine(“FileLock v1.0”);
  8.              Console.WriteLine(“Usage: FileLock <path>”);
  9.          }
  10.          else
  11.          {
  12.              Console.WriteLine(args[0] + ” is Locked” + Environment.NewLine + “Press any key to relase!”);
  13.              using (FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Write, FileShare.Write))
  14.              {
  15.                  fs.Lock(0, fs.Length);
  16.                  Console.ReadKey();
  17.                  fs.Unlock(0, fs.Length);
  18.              }
  19.          }
  20.      }
  21.  }
Advertisements