-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtentions.cs
55 lines (50 loc) · 2.22 KB
/
Extentions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace wp.dll.lib32.textWork
{
internal static class Extentions
{
internal static string GetHash(this string str, Encoding sourceFileEncoding) => new MD5CryptoServiceProvider().ComputeHash(sourceFileEncoding.GetBytes(str)).Aggregate(string.Empty, (current, b) => current + b.ToString("x2"));
internal static string ToFileName(this string input, string replace = null) =>
input
.Replace("\\", replace ?? string.Empty)
.Replace("/", replace ?? string.Empty)
.Replace(":", replace ?? string.Empty)
.Replace("*", replace ?? string.Empty)
.Replace("?", replace ?? string.Empty)
.Replace("<", replace ?? string.Empty)
.Replace(">", replace ?? string.Empty)
.Replace("|", replace ?? string.Empty)
.Replace(".", replace ?? string.Empty)
.Replace("\r\n", replace ?? " ")
.Replace("\r", replace ?? " ")
.Replace("\n", replace ?? " ")
.Replace("\t", replace ?? " ")
.Trim();
internal static StreamWriterExt GetOrCreate(this List<StreamWriterExt> list, string name, DirectoryInfo directory, Encoding encoding)
{
var ext = list.FirstOrDefault(c => c.FileInfo.Name == name);
if (ext == null)
{
ext = new StreamWriterExt(new FileInfo($"{directory.FullName}\\{name}"), encoding);
list.Add(ext);
}
return ext;
}
internal static void CloseAll(this IEnumerable<StreamWriterExt> ieEnumerable)
{
foreach (var streamWriterExt in ieEnumerable)
{
streamWriterExt.StreamWriter.Close();
if (streamWriterExt.Counter == 1)
{
streamWriterExt.FileInfo.MoveTo($"{streamWriterExt.FileInfo.DirectoryName}\\{File.ReadAllText(streamWriterExt.FileInfo.FullName).ToFileName("_")}.0");
}
}
}
}
}