Update UniversalExtensions.cs
This commit is contained in:
parent
f0c201e859
commit
800ec1a349
1 changed files with 32 additions and 17 deletions
|
|
@ -168,28 +168,12 @@ public static class UniversalExtensions
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Remove unsupported characters from string to generate a valid filename
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="name">The string with potentionally unwanted characters</param>
|
|
||||||
/// <param name="replace_char">The character the unwanted characters get replaced with (default: <code>_</code>)</param>
|
|
||||||
/// <returns>A valid filename</returns>
|
|
||||||
public static string MakeValidFileName(this string name, char replace_char = '_')
|
|
||||||
{
|
|
||||||
string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()));
|
|
||||||
string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);
|
|
||||||
|
|
||||||
return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, replace_char.ToString()).Replace('&', replace_char);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Compute the SHA256-Hash for a given file
|
/// Compute the SHA256-Hash for a given file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filePath"></param>
|
/// <param name="filePath"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string ComputeSHA256Hash(this string filePath)
|
public static string ComputeSHA256Hash(string filePath)
|
||||||
{
|
{
|
||||||
using (SHA256 _SHA256 = SHA256.Create())
|
using (SHA256 _SHA256 = SHA256.Create())
|
||||||
{
|
{
|
||||||
|
|
@ -379,4 +363,35 @@ public static class StringExt
|
||||||
|
|
||||||
return value.Length <= maxLength ? value : $"{value.Substring(0, maxLength)}..";
|
return value.Length <= maxLength ? value : $"{value.Substring(0, maxLength)}..";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove unsupported characters from string to generate a valid filename
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The string with potentionally unwanted characters</param>
|
||||||
|
/// <param name="replace_char">The character the unwanted characters get replaced with (default: <code>_</code>)</param>
|
||||||
|
/// <returns>A valid filename</returns>
|
||||||
|
public static string MakeValidFileName(this string name, char replace_char = '_')
|
||||||
|
{
|
||||||
|
string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()));
|
||||||
|
string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);
|
||||||
|
|
||||||
|
return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, replace_char.ToString()).Replace('&', replace_char);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compute the SHA256-Hash for the given string
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filePath"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string ComputeSHA256Hash(string str)
|
||||||
|
{
|
||||||
|
using (SHA256 _SHA256 = SHA256.Create())
|
||||||
|
{
|
||||||
|
return BitConverter.ToString(_SHA256.ComputeHash(Encoding.ASCII.GetBytes(str))).Replace("-", "").ToLowerInvariant();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue