hit tracker


Home » WPF

Bind File System in WPF using FileSystemWatcher in C#

in C-Sharp, Programming, UI, WPF by Balamanigandan B Comments are off

C# is one of the most popular languages today and there are many reasons for this. It has many qualities that make it suitable for development for users at most all levels. Let’s discuss how to efficiently use the AsEnumerable and AsQueryable in C# LINQ.

FileSystemWatcher Class implements to watch for changes in a specified directory. We can watch for changes in files and subdirectories of the specified directory. We can create a component to watch files on a local computer, a network drive, or a remote computer. FileSystemWatcher is perhaps the most important features in .NET used in the workplace today.

Binding File System


The Property FilesCollection has only get access, get can only read information from that private field and return it. But set can only write information in that private field. But in our scenario, we are accessing the Window’s File System, So, we can’t know when the Folder gets update i.e., File Add / Delete / Rename. So, the above View Model fails to update the UI in dynamic.

Let us see how to overcome this problem. The .NET Framework introduces a new feature as like watchdog to monitor the File System, its called as FileSystemWatcher Class under the namespace System.IO. Let us see how to implement

FileSystemWatcher Class


FileSystemWatcher Class has four major property, they are

  • Path
  • NotifyFilter
  • Filter
  • Event Handlers
  • Transaction Control Language


It specifies the filesystem physical path to watch.


Specifies the type of changes to watch for in a file or folder. It supports the following Notification, they are

  • Attributes
  • CreationTime
  • DirectoryName
  • FileName
  • LastAccess
  • LastWrite
  • Security
  • Size


It’s used to determine what files are monitored in a directory. For example

  • *.* – All files (default). An empty string (“”) also watches all files.
  • *.txt – All files with a “txt” extension.
  • My*.txt – All files with a “txt” extension only stats with “My”.
  • M*a.txt – All files with a “txt” extension only stats with “M” and ends with “a”.
  • MyData.txt – Watches only MyData.txt
  • Jellyfish.jpg – Watches only Jellyfish.jpg

Event Handlers

It’s triggers the following events

  • OnChanged – Raises the Changed event.
  • OnCreated – Raises the Created event.
  • OnDeleted – Raises the Deleted event.
  • OnError – Raises the Error event.
  • OnRenamed – Raises the Renamed event.


The component will not watch the specified directory until the Path property has been set and EnableRaisingEvents is true.

Important Notes:

Please note the following when using the FileSystemWatcher class.

  • Hidden files are not ignored.
  • In some systems, FileSystemWatcher reports changes to files using the short 8.3 file name format. For example, a change to “LongFileName.LongExtension” could be reported as “LongFil~.Lon”.
  • This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands.
  • The maximum size you can set for the InternalBufferSize property for monitoring a directory over the network is 64 KB.
  • Running FileSystemWatcher on Windows 98 is not supported.


Conclusion :

This article targets at understanding how efficiently we can Bind File System in WPF using FileSystemWatcher in C#. This article is intended for the beginner/intermediate level. I hope, this post will solve all your basic functionality implementation of FileSystemWatcher in C#. Think Big… Start Small… Do Fast…


in WPF by Selva Kumar Comments are off

In WPF we have lot of controls which we can customize to achieve our desired result. It can be done either through XAML or by creating custom library file also by combining both the way. Here I’m going to customize a ComboBox to accept multiple values only by using XAML file.


Idea behind it

“General idea of this to change the default ItemsPresenter control in the ComboBox to ListBox control which is having the option to accept multiple values”.

The initial code will look like this:

Now we are going to start customizing the look like this:



It will modify the intial Xaml page like this

And also in App.Xaml we will have code like

In this template we need to modify the ItemsPresenter control inside the ControlTemplate to ListBox

Here SelectionMode is the property which helps us to accept multiple values. It accepts three values Single, Multiple & Extended. As the name implies Single will accept single value, Multiple will accept multiple values & Extended will accept values based on ctrl & shift buttons as in Windows default functionality.


Now we will be able to accept multiple values & the next step is to display the selected values in the ComboBox selection. We can achieve this by modifying the ContentPresenter found in the template to ItemsControl.

ItemsControl.ItemsPanel is added to display the selected items in horizontal way.


But again here we need to separate the selected items by some means, so that we can easily distinguish it. For that purpose we can still customize the ItemsControl’s ItemTemplate like this to show and hide a “Comma” block base on the condition.


Now it looks perfect. We have done with the customization.Cheers!!!