Function to shorten the file path for easier reading

Is there any function in C # to mask the file path?

Input : "c: \ users \ Windows \ Downloaded program files \ Folder \ Inside \ example \ file.txt"

Output : "c: \ users \ ... \ example \ file.txt"

+3
source share
7 answers

Jeff Atwood posted a solution to this on his blog, and here it is:

[DllImport("shlwapi.dll", CharSet = CharSet.Auto)] static extern bool PathCompactPathEx([Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags); static string PathShortener(string path, int length) { StringBuilder sb = new StringBuilder(); PathCompactPathEx(sb, path, length, 0); return sb.ToString(); } 

It uses the uncontrolled PathCompactPathEx function to achieve what it wants.

+3
source

It seems less understandable to me. In any case, I do not think that there is such a function. divide it by the \ character and just save the first two slots and the last two slots, and you have it.

Something like this, although this code is not very elegant

  string[] splits = path.Split('\\'); Console.WriteLine( splits[0] + "\\" + splits[1] + "\\...\\" + splits[splits.Length - 2] + "\\" + splits[splits.Length - 1]); 
+7
source

If you want to insert an ellipse depending on the length of the path line, use this code:

 TextRenderer.MeasureText(path, Font, new System.Drawing.Size(Width, 0), TextFormatFlags.PathEllipsis | TextFormatFlags.ModifyString); 

It will change the path in place.

EDIT . Be careful with this method. This breaks the rule by saying that strings in .NET are immutable. In fact, the first parameter of the MeasureText method MeasureText not a ref parameter, which means that no new line can be returned. Instead, the existing row is modified. It would be wary to work on a copy created with

 string temp = String.Copy(path); 
+3
source

Nasruddin's answer was almost correct. Just specify the size of the StringBuilder, in your case:

 [DllImport("shlwapi.dll", CharSet = CharSet.Auto)] static extern bool PathCompactPathEx( [Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags); static string PathShortener(string path, int length) { StringBuilder sb = new StringBuilder(length + 1); PathCompactPathEx(sb, path, length, 0); return sb.ToString(); } 
+3
source

You can use something like:

 public string ShrinkPath(string path, int maxLength) { List<string> parts = new List<string>(path.Split('\\')); string start = parts[0] + @"\" + parts[1]; parts.RemoveAt(1); parts.RemoveAt(0); string end = parts[parts.Count-1]; parts.RemoveAt(parts.Count-1); parts.Insert(0, "..."); while(parts.Count > 1 && start.Length + end.Length + parts.Sum(p=>p.Length) + parts.Count > maxLength) parts.RemoveAt(parts.Count-1); string mid = ""; parts.ForEach(p => mid += p + @"\"); return start+mid+end; } 

Or just use the Olivers solution, which is much simpler :-).

+2
source

I just ran into this problem as long paths became a complete eye pain. This is what I threw together very quickly (carelessly), but he is doing his job.

 private string ShortenPath(string path, int maxLength) { int pathLength = path.Length; string[] parts; parts = label1.Text.Split('\\'); int startIndex = (parts.Length - 1) / 2; int index = startIndex; string output = ""; output = String.Join("\\", parts, 0, parts.Length); decimal step = 0; int lean = 1; do { parts[index] = "..."; output = String.Join("\\", parts, 0, parts.Length); step = step + 0.5M; lean = lean * -1; index = startIndex + ((int)step * lean); } while (output.Length >= maxLength && index != -1); return output; } 

Results

EDIT

The following is an update with the Merlin2001 fixes.

 private string ShortenPath(string path, int maxLength) { int pathLength = path.Length; string[] parts; parts = path.Split('\\'); int startIndex = (parts.Length - 1) / 2; int index = startIndex; String output = ""; output = String.Join("\\", parts, 0, parts.Length); decimal step = 0; int lean = 1; while (output.Length >= maxLength && index != 0 && index != -1) { parts[index] = "..."; output = String.Join("\\", parts, 0, parts.Length); step = step + 0.5M; lean = lean * -1; index = startIndex + ((int)step * lean); } return output; } 
+1
source
  private string ShrinkPath(string path, int maxLength) { var parts = path.Split('\\'); var output = String.Join("\\", parts, 0, parts.Length); var endIndex = (parts.Length - 1); var startIndex = endIndex / 2; var index = startIndex; var step = 0; while (output.Length >= maxLength && index != 0 && index != endIndex) { parts[index] = "..."; output = String.Join("\\", parts, 0, parts.Length); if (step >= 0) step++; step = (step * -1); index = startIndex + step; } return output; } 
+1
source

Source: https://habr.com/ru/post/903207/


All Articles