Shrink File Paths with an Ellipsis in C#

I searched online for a way to shorten a path in .NET. I couldn’t find a solid solution so I had to roll my own. The following method will handle your basic compression needs. It has also been smartened-up to perform some base filename care to reveal the most import characters first. It can handle both Windows and Mac file system structures. It can also properly compact network paths.

The method can also take a custom delimiter of one or more characters. A custom delimiter is nice in cases where an actual ellipsis (the default character) won’t render properly in the UI. Here is what you can expect:

Win Mac Network
…s… …s… …s…
…stripes… …stripes… …stripes…
…stripes.jpg …stripes.jpg …stripes.jpg
…\stripes.jpg …/stripes.jpg …\stripes.jpg
C…\stripes.jpg /…/stripes.jpg \…\stripes.jpg
C:…\stripes.jpg /U…/stripes.jpg \\…\stripes.jpg
C:\Users\chadk\…\stripes.jpg /Users/chadk/Pi…/stripes.jpg \\ps1\Themes\Mi…\stripes.jpg

Read More