Improvements to the OpenFileDialog class

Screenshot of OpenFileDialogEx class running on a Windows Mobile Professional deviceJosé Gallardo Salazar has blogged about an enhanced OpenFileDialog implementation that fixes a number of common complaints with the standard .NET Compact Framework version. The most common complaints being not able to browse outside the “My Documents” folder and the lack of support for Smartphone (Windows Mobile Standard) devices.

As indicated in his blog entry on adding Smartphone support the OpenFileDialogEx class is still a work in progress with many possible areas of improvement. I have taken José’s source code and made a number of changes.

My improvements include:

  • Shell Icon List support – The dialog now uses the same icons as shown in File Explorer. Directories for example may have a custom icon (for example the Games folder shown in the screenshot above).
  • DPI / Resolution awareness – icons change size to match current system settings (i.e. larger icons on higher resolution devices).
  • Support for both Pocket PC and Smartphone – the platform is detected at runtime so a single assembly can alter it’s UI behaviour to run on both platforms. Some UI elements are hidden while running on a Smartphone for example.
  • Back Key support – The Back key on Smartphone Devices now moves up one directory when pressed (just like the builtin File Explorer application on WM6 devices).
  • Sorted Directory Listings – Directory entries are now sorted into alphabetical order. There is provision for the sort order to be easily modified (although this is not yet publicly exposed).
  • InitialDirectory property – This property enables the dialog to default to displaying a directory other than the root of the filesystem.

Sample Application

[Download iconviewer.zip - 22KB]

The sample application available for download demonstrates how to use the OpenFileDialogEx class and demonstrates its cross platform capability. Once you have built the application you should be able to run it on practically any Windows Mobile device and see a file dialog that is tailored to the capabilities of the current device.

One small disadvantage of my changes is the fact that the control is now less “pure”. It relies upon a number of Platform Invoke calls to access some of the system functionality not exposed by the .NET Compact Framework. An interesting note is that .NET Compact Framework version 3.5 removes the need for most of the Platform Invoke code due to new functionality or bug fixes. At this point in time however supporting .NET CF 2.0 is still important to me.

4 Responses to “Improvements to the OpenFileDialog class”

  1. Cocotteseb says:

    Hi,

    It is a really nice sample with a lot of interesting things such as shell icons (!), PPC and smartphone code,…
    I will deeply look into this!

    Thanks!
    Cocotteseb

  2. makis says:

    nice work

  3. poek says:

    Great sample.
    If you want to have multi-filter you can simply change this part:

    foreach (string entry in OrderEnteries(Directory.GetFiles(currentPath, filters[i])))
    {
    ListViewItem item = new ListViewItem(Path.GetFileName(entry));
    item.ImageIndex = shellIconCache[entry];
    item.Tag = entry;
    fileListView.Items.Add(item);
    }

    to this:

    string[] filters = filter.Split(’|');

    for (int i = 0; i < filters.Length; i++)
    {
    foreach (string entry in OrderEnteries(Directory.GetFiles(currentPath, filters[i])))
    {
    ListViewItem item = new ListViewItem(Path.GetFileName(entry));
    item.ImageIndex = shellIconCache[entry];
    item.Tag = entry;
    fileListView.Items.Add(item);
    }
    }

    PS.Sorry for my english.

  4. Somdutta says:

    Just about a perfect solution for anyone who’s trying to save time to market….Thanks

Leave a Reply