-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace uSync.BackOffice.Extensions; | ||
|
||
/// <summary> | ||
/// extensions to manipulate folder path strings. | ||
/// </summary> | ||
public static class PathExtensions | ||
{ | ||
/// <summary> | ||
/// truncate a folder path to only show the last count paths.. | ||
/// </summary> | ||
public static string TruncatePath(this string path, int count = 3, bool includeFile = false) | ||
{ | ||
if (string.IsNullOrWhiteSpace(path)) return path; | ||
|
||
var result = ""; | ||
|
||
var fullPath = includeFile ? path : Path.GetDirectoryName(path); | ||
if (string.IsNullOrWhiteSpace(fullPath)) return fullPath ?? string.Empty; | ||
|
||
var bits = fullPath.Split([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]); | ||
|
||
foreach (var item in bits.Reverse().Take(count)) | ||
{ | ||
if (Path.IsPathRooted(item)) continue; | ||
|
||
result = $"{Path.DirectorySeparatorChar}{item}{result}"; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters