locked
Adjust file path RRS feed

  • Question

  • Dears,

    In my application user can select a file via openfile dialog and then I display the

    file path in a textbox control. I want to adjust the file path to be shown in the textbox width. For example if the file path is

     : C:\Program files \folder1\folder2\folder3\folder4\folder5\selectedfile.txt

    the application shows this path which is fit to the textbox width:

    C:\program files \...\folder5\selectedfile.txt

     

    Please let me know if there is any method which can do this conversion.

    Thanks in advance,

    Saeideh

    Monday, July 2, 2007 1:57 PM

Answers

  •  

    Here's one solution:

     

    Code Snippet

    [C#]

    public String strEllipsis(String inString, int w, Font f)
     {
       String toReturn = inString;
       System.Drawing.Size s = new Size(w,0);
       TextRenderer.MeasureText(toReturn, f, s, TextFormatFlags.PathEllipsis | TextFormatFlags.ModifyString);
       return toReturn;
     }

    private void button1_Click(object sender, EventArgs e)
     {
       textBox1.Text = strEllipsis("C:\\Program files\\folder1\\folder2\\folder3\\folder4\\folder5\\selectedfile.txt", textBox1.Width, textBox1.Font); 
     }

     

     

     

    Line-breaks may appear in this post; file-name has been escaped for inclusion in source code. 

     

    HTH

     

     

    Monday, July 2, 2007 2:48 PM

All replies

  • Likely the closest thing you will find for doing something like this is the Win32 API call GetShortPathName which gives you a system generated string that is not as descriptive as what you are looking for... instead, to start yanking out folders from the middle you’ll have to do it on your own with something ugly like this:

     

    Code Snippet

          public string ReducePath(string path, int desiredLength)

          {

             string newPath = "";

     

             string[] parts = path.Split('\\');

     

             int startEnd = 0;

             int endStart = parts.Length - 1;

             //Starts with 3 so as to account for the added ..\

             int pathLength = 3;

             bool toggle = true;

     

             while (startEnd != endStart && pathLength < desiredLength)

             {

                if (toggle)

                {

                   pathLength += parts[startEnd++].Length + 1;

                }

                else

                {

                   pathLength += parts[endStart--].Length + 1;

                }

               

                toggle = !toggle;

             }

     

             //Add leading directories

             for (int x = 0; x < startEnd; x++)

             {

                newPath += parts[x] + "\\";

             }

     

             //Add mid point marker

             newPath += "..\\";

     

             //Add trailing directories and file name

             for (int x = endStart; x <= parts.Length-1; x++)

             {

                newPath += parts[x];

     

                if (x != parts.Length - 1)

                {

                   newPath += "\\";

                }

             }

     

             return newPath;

          }

     

    Monday, July 2, 2007 2:34 PM
  •  

    Here's one solution:

     

    Code Snippet

    [C#]

    public String strEllipsis(String inString, int w, Font f)
     {
       String toReturn = inString;
       System.Drawing.Size s = new Size(w,0);
       TextRenderer.MeasureText(toReturn, f, s, TextFormatFlags.PathEllipsis | TextFormatFlags.ModifyString);
       return toReturn;
     }

    private void button1_Click(object sender, EventArgs e)
     {
       textBox1.Text = strEllipsis("C:\\Program files\\folder1\\folder2\\folder3\\folder4\\folder5\\selectedfile.txt", textBox1.Width, textBox1.Font); 
     }

     

     

     

    Line-breaks may appear in this post; file-name has been escaped for inclusion in source code. 

     

    HTH

     

     

    Monday, July 2, 2007 2:48 PM
  • Hot dang Craig, I like your method a heck of a lot better than mine.
    Monday, July 2, 2007 3:10 PM
  • Just luck Brendan, .NET is so huge, it makes finding these little things a bit of a voyage.  I seem to remember somebody at TechEd using the MeasureText method, since then it has been in my in my collection of code snippets.   Mind you, I do like your method - it demonstrates a good algorithmic approach and has some good learning material too!

    Monday, July 2, 2007 4:13 PM