views:

130

answers:

5

I have a fixed-length field I'd like to display path information in. I thought in .NET there is a method that will abbreviate long path names to fit in fixed length fields by inserting ellipsis, for example "......\myfile.txt". I can't for the life of me find this method.

A: 

I don't know of a method to do that automatically, but you could easily create one that iether used Substring() and LastIndexOf("\") or the System.IO.Path.GetFileName() method to grab just the filename, then prepend your dot's.

palehorse
A: 

How about:

string longPath = @"c:\somewhere\myfile.txt";
string shortPath = @"..\" + Path.GetFileName(longPath);
Philip Wallace
+3  A: 

From this Coding Horror blog post there's a Windows API call PathCompactPathEx you can use.

ChrisF
Useful article by Jeff.
Philip Wallace
+1  A: 

You're thinking of the StringTrimming.EllipsisPath enumeration constant, which can be used with a StringFormat to draw a trimmed path using Graphics.DrawString. However, .Net itself has no method which will return a trimmed path.

SLaks
A: 

I found an easy-to-use class derived from TextBox here: EllipsisTextBox that encapsulates StringTrimming.EllipsisPath. Works for me!

chuckhlogan