Click here to Skip to main content
15,892,965 members
Articles / Desktop Programming / Win32

Notifying Windows Explorer about files in use

Rate me:
Please Sign up or sign in to vote.
4.84/5 (47 votes)
26 Mar 2013LGPL33 min read 84.9K   635   132  
How to notify Windows Explorer about which files are used and locked by your application.
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 Service 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)


Written By
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Science and Technology, Poland.

My interests: gardening, reading, programming, drawing, Japan, Spain.

Comments and Discussions