Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  


File Name Sorter in .NET
By default, windows (some versions) sort file names alpha-numerically instead of alphabetically.

To replicate that in .NET, use this FileNameSorter class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

/// <summary>
/// Implements the same filename sort behavior as windows explorer
/// </summary>
public class FileNameSorter : IComparer<string>
/// <summary>
/// This function compares two file names so they sort the same way as in windows explorer (alpha-numeric) instead of alphabetically.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int StrCmpLogicalW(String x, String y);
public int Compare(string x, string y)
return StrCmpLogicalW(x, y);

Created By: amos 4/7/2015 11:34:22 AM
Updated: 9/12/2017 4:01:31 PM