5,424,116 members and growing! (18,370 online)
Email Password   helpLost your password?
Desktop Development » Shell and IE programming » Shell Programming     Intermediate License: The Common Public License Version 1.0 (CPL)

Notifying Windows Explorer about files in use

By Lukasz Swiatkowski

How to notify Windows Explorer about which files are used and locked by your application.
C# (C# 2.0, C# 3.0, C#), Windows (Windows, Win2K, WinXP, Win2003, Vista, TabletPC, Embedded), .NET (.NET, .NET 3.5, .NET 3.0, .NET 2.0), Win32, COM, Dev

Posted: 8 May 2008
Updated: 8 May 2008
Views: 6,115
Bookmarked: 39 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
22 votes for this Article.
Popularity: 6.21 Rating: 4.63 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 4.5%
3
7 votes, 31.8%
4
14 votes, 63.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

While doing some files-related operations like renaming, moving or deleting, many of you might encounter this kind of message:

Error — file is in use

This is the standard way Windows Explorer notifies the user that some file is in use, and therefore it cannot be “changed” in any way (that is renamed, moved, deleted, etc.)

Of course there is the way for an application to notify Windows Explorer about which files are in use. For example Microsoft Office applications do that. When Windows Explorer is notified, the message saying that the file is in use looks different:

error2.png

This article explains how to write an application which notifies Windows Explorer about used files.

How to do it?

The easiest way for a developer is to implement a COM interface named IFileIsInUse[^]. Unfortunately, this interface is available only in Windows Vista. Luckily, there is another way. I show an example demonstrating what Windows Explorer (e.g. in Windows XP) does when the user tries to delete a file which is in use. In the following example an entry abc=“123” will mean that the default value of the abc registry key has to be equal to 123.

  1. At first, the Windows Explorer checks if there is an entry for the file in the Running Object Table (ROT[^]). Using C# terminology one could say that the ROT is a kind of globally accessible object whose type would be Dictionary<String, ComObject> (of course it is not, because it is not a .NET object).
  2. If there is an appropriate entry for the file, it checks if the COM object implements the IOleObject interface.
  3. If the interface is implemented, it invokes its GetUserClassID(ref Guid userClassId) method. Let's assume that the method assigns a {abcdef12-ca3b-a654-e640-bf50b3aba521} GUID to the userClassId parameter.
  4. Then it checks if the following keys are in the registry (with their default values set):
    HKCR\CLSID\{abcdef12-ca3b-a654-e640-bf50b3aba521}\ProgID=“ApplicationName”
    HKCR\ApplicationName\CLSID=“{abcdef12-ca3b-a654-e640-bf50b3aba521}”
    HKCR\ApplicationName\shell\Open\command=“path to the application exe file”
  5. Next, in order to display application name in the message box, it reads the description of the file. The file description can be set using the AssemblyTitle attribute.

If everything was OK, Windows Explorer shows a message box (like the second one from this article) with the name of the application.

Using the code

I have created a FileLocker class which provides methods to notify Windows Explorer, which files are currently used by the application. The class implements the IDisposable interface, so it can be comfortably used with the using keyword. The sample usage is shown on the following code:

// GUID, which must be unique, used to register an application in the registry
[assembly: Guid("{abcdef12-ca3b-a654-e640-bf50b3aba521}")]
// Application title which will be shown by the Windows Explorer
[assembly: AssemblyTitle("FileLockerApplication")]

...

// registers the application in the registry
FileLocker.Register();

string path = "some_file.txt";
// creates or opens the file and locks it
using (FileStream fs = File.Open(path, FileMode.OpenOrCreate,
                                 FileAccess.ReadWrite, FileShare.None))
// notifies Windows Explorer that the file is in use
using (FileLocker fl = new FileLocker(path))
{
    // here might be some code
}
// deletes the file
File.Delete(path);

// unregisters the application from the registry
FileLocker.Unregister();

The appropriate keys in the registry are created by the FileLocker.Register() method, and deleted by the FileLocker.Unregister() method. They require a permission to write to the registry, so it is best to invoke them from the application during installation/uininstallation process. To successfully register there is also needed a Guid attribute applied to the assembly.

FileLocker class members

Static methods

  • void Register() — Registers the FileLocker. Invoke it from the application during the installation process.
  • void Unregister() — Unregisters the FileLocker. Invoke it from the application during the uninstallation process.

Constructors and instance methods

  • FileLocker(FileInfo fileInfo) & FileLocker(string path) — Initializes a new instance of the FileLocker class and notifies Windows Explorer that the specified file is currently used by the application.
  • void Dispose() — Cancels the notification and disposes the object.

History

  • 1.0 (09.05.2008) — first version.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)

About the Author

Lukasz Swiatkowski


Mvp
I'm studying computer science at Wroclaw University of Technology, Poland.

My interests: .NET, reading, programming, drawing, OBE and LD, Japan, yoga, tai-chi.
My favourite movie: "Star Trek: First Contact".

My website: www.lukesw.net
My email: lukasz.swiatkowski/*at-sign*/gmail.com.
Location: Poland Poland

Other popular Shell and IE programming articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralMean CodememberJonathan C Dickinson1:14 14 May '08  
GeneralRe: Mean CodemvpLukasz Swiatkowski6:51 17 May '08  
GeneralRe: Mean CodememberJonathan C Dickinson6:06 18 May '08  
GeneralRe: Mean CodemvpLukasz Swiatkowski6:12 18 May '08  
GeneralGood Jobmemberklaus_b20:33 13 May '08  
GeneralRe: Good JobmvpLukasz Swiatkowski6:49 17 May '08  
GeneralNice manmvpSacha Barber3:59 9 May '08  
GeneralRe: Nice manmvpLukasz Swiatkowski8:17 9 May '08  
GeneralRe: Nice manmvpSacha Barber5:43 10 May '08  
GeneralRe: Nice manmvpLukasz Swiatkowski12:55 11 May '08  
GeneralRe: Nice manmvpSacha Barber22:07 11 May '08  
GeneralRe: Nice manmvpLukasz Swiatkowski9:37 12 May '08  
GeneralRe: Nice manmvpSacha Barber23:17 12 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 May 2008
Editor:
Copyright 2008 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project