C# Path Ellipsis without Win32 API call revisited

Posted by casterle on Stack Overflow See other posts from Stack Overflow or by casterle
Published on 2010-04-14T19:31:47Z Indexed on 2010/04/14 20:23 UTC
Read the original article Hit count: 503

Filed under:
|
|
|

I was searching for a way to insert an ellipsis in a C# path, and found an answer here on stackoverflow: http://tinyurl.com/y6rmdfr

Using the RTM versions of VS2010 and .Net 4.0, I was unable to get the suggested method to work. I searched the 'Net and found example code that uses the same method, but it failed in the same way.

You can see the string I'm trying to shorten in my code below.

After calling the MeasureText method, both the input string (OriginalName) and the output string (ellipsisedName) look like this:

d:\abcd\efgh\ijkl\mnop\qrst\...\test.txt\0F\GHIJ\KLMN\OPQR\STIV\WXYZ\test.txt

Two problems:

1) The resulting string is narfed (the path is truncated as expected, but is followed by what looks like a C-style terminating null and a chunk of the original path).

2) My original string is changed to be identical to the output string.

Am I doing something wrong?

namespace WindowsFormsApplication2 {
   public partial class Form1 : Form {
      public Form1()
      {
         InitializeComponent();

         string OriginalPath = @"d:\abcd\efgh\ijkl\mnop\qrst\uvwx\yzAB\CDEF\GHIJ\KLMN\OPQR\STIV\WXYZ\test.txt";
         string ellipsisedPath = OriginalPath;

         Size proposedSize = new Size(label1.Width, label1.Height);

         TextRenderer.MeasureText(ellipsisedPath, label1.Font, proposedSize, TextFormatFlags.ModifyString | TextFormatFlags.PathEllipsis);
      }
   }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .net-4