Microsoft KB Archive/873196

= The FileSystemWatcher class ignores the uppercase characters in a file name =

Article ID: 873196

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * 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:   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(&quot;The file '&quot; & e.FullPath & &quot;' is created&quot;) 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(&quot;The file '&quot; + e.FullPath + &quot;' is created&quot;); }   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(&quot;C:\&quot;, e.Name) Dim dir As String For Each dir In ActualFileName MessageBox.Show(&quot;The file '&quot; & dir & &quot;' is created&quot;) Next End Sub Visual C# .NET code private static void OnChanged(object source, FileSystemEventArgs e) { string[] ActualFileName = Directory.GetFiles (&quot;C:\\&quot;, e.Name); foreach (string dir in ActualFileName) {   MessageBox.Show(&quot;The file '&quot; + dir + &quot;' is created&quot;); } }  On the Build menu, click Build Solution. On the Debug menu, click Start. Create a text file that is named MYAPP1.txt, and then save the file on drive C. Close the file. You see a message box that contains the following message:

The file 'C:\MYAPP1.txt' is created

Notice that the name of the file in the message is displayed as MYAPP1.txt.

<div class="moreinformation_section">

Steps to reproduce the behavior
 Start Visual Studio .NET.</li> 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.</li>  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; </li> In Design view, double-click the Form1 form. The Form1_Load procedure is added to the Form1.vb file.</li>  Add the following code to the Form1_Load procedure:

Visual Basic .NET code Dim watcher As New FileSystemWatcher watcher.Path = &quot;c:\&quot;

' Only watch text files. watcher.Filter = &quot;*.txt&quot;

' Add event handlers. AddHandler watcher.Created, AddressOf OnCreate

' Start watching. watcher.EnableRaisingEvents = True Visual C# .NET code FileSystemWatcher watcher = new FileSystemWatcher ; watcher.Path = &quot;C:\\&quot;;

// Only watch text files. watcher.Filter= &quot;*.txt&quot;;

// Add event handlers. watcher.Created +=new FileSystemEventHandler(OnChanged);

// Start watching. watcher.EnableRaisingEvents =true; </li>  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(&quot;The file '&quot; & e.FullPath & &quot;' is created&quot;) 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(&quot;The file '&quot; + e.FullPath + &quot;' is created&quot;); } </li> On the Build menu, click Build Solution.</li> 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.</li> In Notepad, create a text file, and save the file as MYAPP.txt on drive C.</li> Close the file. You see a message box that contains the following message:

The file 'C:\myapp.txt' is created

Notice that the name of the file in the message is displayed as myapp.txt instead of MYAPP.txt.</li></ol>

<div class="references_section">