Click here to Skip to main content
12,450,981 members (23,320 online)
Click here to Skip to main content

Stats

53.1K views
555 downloads
131 bookmarked
Posted

Notifying Windows Explorer about files in use

, 26 Mar 2013 LGPL3
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)

Share

About the Author

Lukasz Swiatkowski
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Technology, Poland.

My interests: reading, programming, drawing, Japan, yoga, tai-chi.

My websites: luminous.codeplex.com, www.lukesw.net

You may also be interested in...

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