The FileSystemWatcher class ignores the uppercase characters in a file name (873196)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

SYMPTOMS

In Microsoft Visual Studio .NET, you may use the FileSystemWatcher class monitor changes that are made to the file system. When a file is changed, created, or deleted, the FileSystemWatcher class returns the name of the file in lowercase letters even if the actual file name contains uppercase letters.

RESOLUTION

To resolve this problem, use the System.IO.Directory class to retrieve the actual file name. To do this, follow these steps:
  1. Locate the following code in the Form1.vb file:

    Microsoft Visual Basic .NET code
    ' Define the event handlers.
    Private Shared Sub OnCreate(ByVal source As Object, ByVal e As FileSystemEventArgs)
        ' Specify what task is performed when a file is changed, created, or deleted.
        MessageBox.Show("The file '" & e.FullPath & "' is created")
    End Sub
    Microsoft Visual C# .NET code
    //Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
      //Displays a message when a file is created.
      MessageBox.Show("The file '" + e.FullPath  + "' is created");		
    }
    
  2. Replace the code that you located in step 1 with the following code:

    Visual Basic .NET code
    Private Shared Sub OnCreate(ByVal source As Object, ByVal e As FileSystemEventArgs)
       Dim ActualFileName As String() = Directory.GetFiles("C:\", e.Name)
       Dim dir As String
       For Each dir In ActualFileName
         MessageBox.Show("The file '" & dir & "' is created")
       Next
    End Sub
    Visual C# .NET code
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        string[] ActualFileName = Directory.GetFiles ("C:\\", e.Name);
        foreach (string dir in ActualFileName)
        {
    	MessageBox.Show("The file '" + dir + "' is created");
        }
    }
  3. On the Build menu, click Build Solution.
  4. On the Debug menu, click Start.
  5. Create a text file that is named MYAPP1.txt, and then save the file on drive C.
  6. Close the file. You see a message box that contains the following message:
    The file 'C:\MYAPP1.txt' is createdNotice that the name of the file in the message is displayed as MYAPP1.txt.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Visual Studio .NET.
  2. Create a Windows application by using Visual Basic .NET. By default, the Form1.vb file is created. If you use Visual C# .NET, the Form1.cs file is created.
  3. Add the following code at the beginning of the Form1.vb file:

    Visual Basic .NET code
    Imports System.IO
    Visual C# .NET code
    using System.IO;
  4. In Design view, double-click the Form1 form. The Form1_Load procedure is added to the Form1.vb file.
  5. Add the following code to the Form1_Load procedure:

    Visual Basic .NET code
    Dim watcher As New FileSystemWatcher
    watcher.Path = "c:\"
    
    ' Only watch text files.
    watcher.Filter = "*.txt"
    
    ' Add event handlers.
    AddHandler watcher.Created, AddressOf OnCreate
    
    ' Start watching.
    watcher.EnableRaisingEvents = True
    
    Visual C# .NET code
    FileSystemWatcher watcher = new FileSystemWatcher ();
    watcher.Path =  "C:\\";
    
    // Only watch text files.
    watcher.Filter= "*.txt";
    
    // Add event handlers.	 
    watcher.Created +=new FileSystemEventHandler(OnChanged);
    
    // Start watching.
    watcher.EnableRaisingEvents =true;
  6. Add the following code at the end of the Form1 class:
    Visual Basic .NET code
    'Define the event handlers.
     Private Shared Sub OnCreate(ByVal source As Object, ByVal e As FileSystemEventArgs)
          ' Displays a message when a file is created.
          MessageBox.Show("The file '" & e.FullPath & "' is created")
     End Sub
    
    Visual C# .NET code
    //Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        //Displays a message when a file is created.
        MessageBox.Show("The file '" + e.FullPath  + "' is created");		
    }
    
  7. On the Build menu, click Build Solution.
  8. On the Debug menu, click Start. This sample application will display a message box when you create and save a text (.txt) file on drive C.
  9. In Notepad, create a text file, and save the file as MYAPP.txt on drive C.
  10. Close the file. You see a message box that contains the following message:
    The file 'C:\myapp.txt' is createdNotice that the name of the file in the message is displayed as myapp.txt instead of MYAPP.txt.

Modification Type:MajorLast Reviewed:7/21/2004
Keywords:kbFileSystems kbcode kbtshoot kbprb KB873196 kbAudDeveloper