/// <summary> /// Path to check for invalid characters /// </summary> /// <param name="IOPath"></param> /// <returns></returns> public static String SafeFileName(this String IOPath) { foreach (Char InvalidChar in Path.GetInvalidFileNameChars()) { IOPath = IOPath.Replace(InvalidChar.ToString(), String.Empty); } return(IOPath); }
/// <summary> /// 获取安全目录名(去除目录名非法字符) /// </summary> /// <returns></returns> public static string GetSafeDirectoryName(string directoryname) { StringBuilder SafeDirectoryName = new StringBuilder(directoryname); foreach (char InvalidChar in Path.GetInvalidPathChars()) { SafeDirectoryName.Replace(InvalidChar.ToString(), string.Empty); } return(SafeDirectoryName.ToString()); }
/// <summary> /// 获取安全文件名(去除文件名非法字符) /// </summary> /// <returns></returns> public static string GetSafeFileName(string filename) { StringBuilder SafeFileName = new StringBuilder(filename); foreach (char InvalidChar in Path.GetInvalidFileNameChars()) { SafeFileName.Replace(InvalidChar.ToString(), string.Empty); } return(SafeFileName.ToString()); }
// Add value to list private static bool AppendValue(string value, List <string> domains) { string domain = value .Replace("www.", "").ToLower(); // Check for invalid chars foreach (char InvalidChar in InvalidChars) { if (domain.Contains(InvalidChar.ToString())) { return(false); } } // Remove search results if ( domain.Contains("google") || domain.Contains("bing") || domain.Contains("yandex") || domain.Contains("duckduckgo")) { return(false); } // If cookie value if (domain .StartsWith(".")) { domain = domain.Substring(1); } // Get hostname from url try { domain = new Uri(domain).Host; } catch (UriFormatException) { } // Remove .com, .org domain = System.IO.Path.GetFileNameWithoutExtension(domain); domain = domain.Replace(".com", "").Replace(".org", ""); // Check if domain already exists in list foreach (string domainValue in domains) { if (domain.ToLower().Replace(" ", "").Contains(domainValue.ToLower().Replace(" ", ""))) { return(false); } } // Convert first char to upper-case domain = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(domain); domains.Add(domain); return(true); }
/// <summary> /// Verify the path exists and create it if need be /// </summary> /// <param name="IOPath">Path to check</param> /// <param name="Create">Create it if the path does not exist</param> /// <returns></returns> public static String SafeFolder(this String IOPath, Boolean Create = true) { foreach (Char InvalidChar in Path.GetInvalidPathChars()) { IOPath = IOPath.Replace(InvalidChar.ToString(), String.Empty); } String[] Directories = (IOPath.Contains(":\\") ? IOPath.Substring(3) : IOPath).Split('\\'); IOPath = Path.GetPathRoot(IOPath); foreach (String _Directory in Directories) { IOPath = Path.Combine(IOPath, _Directory.SafeFileName()); if (!File.Exists(IOPath) && !Directory.Exists(IOPath) && Create) { Directory.CreateDirectory(IOPath); } } return(IOPath); }