Click here to Skip to main content
Licence 
First Posted 21 Sep 2005
Views 28,682
Bookmarked 18 times

FileSystemWatcher - How To Get The Correctly Cased File Name

By | 21 Sep 2005 | Article
How to convert, the lower cased file name returned by the FileSystemWatcher class into the casing as displayed by Windows Explorer.

Introduction

When I started using the FileSystemWatcher class, I soon discovered that the file names it returns from its events are in lowercase rather than the case you would see in Windows Explorer e.g. filename.txt rather than FileName.txt. If you want to perform an action on the file such as moving the file, the file name in its new location will be in lowercase.

I searched around and found that other people also had the same issue, but I could not find a suitable solution. I came up with a solution where I use the Directory.GetFiles(directoryName, fileName) to return a FileInfo object with the correctly cased file name.

This is an example of an event handler for the FileSystemWatcher.Created event:

private void watcher_OnCreated(object source, FileSystemEventArgs e)
{
    FileInfo file = new FileInfo(e.FullPath);

    //watcher always returns the file name in lower case
    //modify the file object to correct this
    file = GetCorrectlyCasedFileInfo(file);

    file.MoveTo(Path.Combine(@"C:\AnotherDirectory", file.Name));
}

The GetCorrectlyCasedFileInfo function takes the FileInfo object with the lowercase name and returns a FileInfo object with the correctly cased file name. There are a few checks to make sure the file exists (this shouldn't occur, but better to check than not), if it does not exist then the original FileInfo object is returned:

private FileInfo GetCorrectlyCasedFileInfo(FileInfo file)
{
    if(Directory.Exists(file.DirectoryName))
    {
        string[] fileNames = 
                  Directory.GetFiles(file.DirectoryName, file.Name);
        
        if((fileNames.Length > 0)
            && (file.FullName.ToLower().Equals(fileNames[0].ToLower())))
        {
            //instantiate a new FileInfo object with 
            //the correctly cased name
            file = new FileInfo(fileNames[0]);
        }
    }

    return file;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

peranb



United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDirectory Names Also PinmemberRad1cal0:01 31 Oct '06  
QuestionWhy not... Pinmemberleppie7:09 21 Sep '05  
AnswerRe: Why not... Pinsussperanb10:41 21 Sep '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 21 Sep 2005
Article Copyright 2005 by peranb
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid