From a30f565b947149ca08c30fb34c0f6b33b0ff21a2 Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 23 May 2022 12:35:50 +0300 Subject: [PATCH] Code refactoring --- ShareX.HelpersLib/Helpers/FileHelpers.cs | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/FileHelpers.cs b/ShareX.HelpersLib/Helpers/FileHelpers.cs index 1c6f4ac65..3f842ebac 100644 --- a/ShareX.HelpersLib/Helpers/FileHelpers.cs +++ b/ShareX.HelpersLib/Helpers/FileHelpers.cs @@ -190,6 +190,18 @@ public static string GetAbsolutePath(string path) return Path.GetFullPath(path); } + public static string GetPathRoot(string path) + { + int separator = path.IndexOf(Path.DirectorySeparatorChar); + + if (separator > 1 && path[separator - 1] == ':') + { + return path.Remove(separator + 1); + } + + return ""; + } + public static string SanitizeFileName(string fileName, string replaceWith = "") { char[] invalidChars = Path.GetInvalidFileNameChars(); @@ -200,24 +212,17 @@ private static string SanitizeFileName(string fileName, string replaceWith, char { fileName = fileName.Trim(); - if (string.IsNullOrEmpty(replaceWith)) + foreach (char c in invalidChars) { - return new string(fileName.Where(c => !invalidChars.Contains(c)).ToArray()); + fileName = fileName.Replace(c.ToString(), replaceWith); } - else - { - foreach (char invalidFileNameChar in invalidChars) - { - fileName = fileName.Replace(invalidFileNameChar.ToString(), replaceWith); - } - return fileName.Replace(replaceWith + replaceWith, replaceWith); - } + return fileName; } public static string SanitizePath(string path, string replaceWith = "") { - string root = Path.GetPathRoot(path); + string root = GetPathRoot(path); if (!string.IsNullOrEmpty(root)) {