Click here to Skip to main content
Click here to Skip to main content

Notifying Windows Explorer about files in use

By , 26 Mar 2013
 
FileLockerApplication.zip
FileLockerApplication
FileLockerApplication.exe
src
Properties
using System;
using System.IO;
using LukeSw.IO;

namespace FileLockerApplication
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Registering the application.");
            try
            {
                FileLocker.Register();
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot register - cannot modify the registry.");
                Console.WriteLine(e);
                return;
            }

            Console.Write("Enter the filename: ");
            string file = Console.ReadLine();
            if (string.IsNullOrEmpty(file))
            {
                Console.WriteLine("Entered an empty string. Exiting.");
                return;
            }
            try
            {
                Console.WriteLine("Creating/opening the file.");
                using (FileStream fs = File.Open(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                {
                    Console.Write("You can try to delete the file. Then press the enter.");
                    Console.ReadLine();
                    using (FileLocker fl = new FileLocker(file))
                    {
	                Console.Write("Windows Explorer has been notifies about used file. Try to delete it now.");
                        Console.Write("Press enter to cancel the notification.");
                        Console.ReadLine();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot create or lock the file.");
                Console.WriteLine(e);
            }
            finally
            {
                Console.WriteLine("Unregistering the application.");
                try
                {
                    FileLocker.Unregister();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Cannot unregister - cannot modify the registry.");
                    Console.WriteLine(e);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

About the Author

Lukasz Swiatkowski
Software Developer
Poland Poland
Member
I am a graduate of Wroclaw University of Technology, Poland.
 
My interests: reading, programming, drawing, Japan, yoga, tai-chi.
 
My website: www.lukesw.net

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 27 Mar 2013
Article Copyright 2008 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid