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
2009-11-19 15:47:19
A:
How about:
string longPath = @"c:\somewhere\myfile.txt";
string shortPath = @"..\" + Path.GetFileName(longPath);
Philip Wallace
2009-11-19 15:47:41
+3
A:
From this Coding Horror blog post there's a Windows API call PathCompactPathEx
you can use.
ChrisF
2009-11-19 15:49:31
Useful article by Jeff.
Philip Wallace
2009-11-19 15:53:05
+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
2009-11-19 15:50:19
A:
I found an easy-to-use class derived from TextBox here: EllipsisTextBox that encapsulates StringTrimming.EllipsisPath. Works for me!
chuckhlogan
2009-11-19 16:18:46