<p>Are there any automatic methods for trimming a path string in .NET?</p>

<p>For example:</p>

<pre><code>C:\Documents and Settings\nick\My Documents\Tests\demo data\demo data.emx
</code></pre>

<p>becomes</p>

<pre><code>C:\Documents...\demo data.emx
</code></pre>

<p>It would be particularly cool if this were built into the Label class, and I seem to recall it is--can't find it though!</p>

[{'answer_id': 20492, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': '<p>What you are thinking on the label is that it will put ... if it is longer than the width (not set to auto size), but that would be</p>\n\n<pre><code>c:\\Documents and Settings\\nick\\My Doc...\n</code></pre>\n\n<p>If there is support, it would probably be on the Path class in System.IO</p>\n'}, {'answer_id': 20493, 'author': 'denis phillips', 'author_id': 748, 'author_profile': 'https://Stackoverflow.com/users/748', 'pm_score': 0, 'selected': False, 'text': '<p>You could use the System.IO.Path.GetFileName method and append that string to a shortened System.IO.Path.GetDirectoryName string.</p>\n'}, {'answer_id': 20494, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 2, 'selected': False, 'text': '<p>Not hard to write yourself though:</p>\n\n<pre><code>    public static string TrimPath(string path)\n    {\n        int someArbitaryNumber = 10;\n        string directory = Path.GetDirectoryName(path);\n        string fileName = Path.GetFileName(path);\n        if (directory.Length &gt; someArbitaryNumber)\n        {\n            return String.Format(@"{0}...\\{1}", \n                directory.Substring(0, someArbitaryNumber), fileName);\n        }\n        else\n        {\n            return path;\n        }\n    }\n</code></pre>\n\n<p>I guess you could even add it as an extension method.</p>\n'}, {'answer_id': 20495, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 4, 'selected': True, 'text': "<p>Use <strong>TextRenderer.DrawText</strong> with <strong>TextFormatFlags.PathEllipsis</strong> flag</p>\n\n<pre><code>void label_Paint(object sender, PaintEventArgs e)\n{\n  Label label = (Label)sender;\n  TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.ClientRectangle, label.ForeColor, TextFormatFlags.PathEllipsis);\n}\n</code></pre>\n\n<blockquote>\n  <p>Your code is 95% there. The only\n  problem is that the trimmed text is\n  drawn on top of the text which is\n  already on the label.</p>\n</blockquote>\n\n<p>Yes thanks, I was aware of that. My intention was only to demonstrate use of <code>DrawText</code> method. I didn't know whether you want to manually create event for each label or just  override <code>OnPaint()</code> method in inherited label. Thanks for sharing your final solution though.</p>\n"}, {'answer_id': 20592, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 2, 'selected': False, 'text': '<p>@ <a href="https://stackoverflow.com/questions/20467?sort=votes#20495">lubos hasko</a> Your code is 95% there.  The only problem is that the trimmed text is drawn on top of the text which is already on the label.  This is easily solved:</p>\n\n<pre><code>    Label label = (Label)sender;\n    using (SolidBrush b = new SolidBrush(label.BackColor))\n        e.Graphics.FillRectangle(b, label.ClientRectangle);\n    TextRenderer.DrawText(\n        e.Graphics, \n        label.Text, \n        label.Font, \n        label.ClientRectangle, \n        label.ForeColor, \n        TextFormatFlags.PathEllipsis);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/20467', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1490/']