-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileUtils.cs
201 lines (181 loc) · 6.62 KB
/
FileUtils.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace DNNpackager
{
public class FileUtils
{
public static void SaveFile(string fullFileName, string data)
{
var buffer = StrToByteArray(data);
SaveFile(fullFileName, buffer);
}
public static void SaveFile(string fullFileName, byte[] buffer)
{
if (File.Exists(fullFileName))
{
File.SetAttributes(fullFileName, FileAttributes.Normal);
}
FileStream fs = null;
try
{
fs = new FileStream(fullFileName, FileMode.Create, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
var ms = ex.ToString();
// ignore, stop eror here, not important if locked.
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
}
public static string ReadFile(string filePath)
{
StreamReader reader = null;
string fileContent;
try
{
if (!File.Exists(filePath)) return "";
reader = File.OpenText(filePath);
fileContent = reader.ReadToEnd();
}
catch (Exception ex)
{
var ms = ex.ToString();
// ignore, stop eror here, not important if locked.
fileContent = "";
}
finally
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
return fileContent;
}
public static string FormatFolderPath(string folderPath)
{
if (String.IsNullOrEmpty(folderPath) || String.IsNullOrEmpty(folderPath.Trim()))
{
return "";
}
return folderPath.EndsWith("/") ? folderPath : folderPath + "/";
}
public static byte[] StrToByteArray(string str)
{
if (str == null) str = "";
var encoding = new UTF8Encoding();
return encoding.GetBytes(str);
}
/// <summary>
/// Convert input stream to UTF8 string, can be used for text files.
/// </summary>
/// <param name="InpStream"></param>
/// <returns></returns>
public static string InputStreamToString(Stream InpStream)
{
// Create a Stream object.
// Find number of bytes in stream.
var strLen = Convert.ToInt32(InpStream.Length);
// Create a byte array.
var strArr = new byte[strLen];
// Read stream into byte array.
InpStream.Read(strArr, 0, strLen);
// Convert byte array to a text string.
var strmContents = Encoding.UTF8.GetString(strArr);
return strmContents;
}
/// <summary>
/// Convert input stream to base-64 string, can be used for image/binary files.
/// </summary>
/// <param name="InpStream"></param>
/// <returns></returns>
public static string Base64StreamToString(Stream InpStream)
{
// Create a Stream object.
// Find number of bytes in stream.
var strLen = Convert.ToInt32(InpStream.Length);
// Create a byte array.
var strArr = new byte[strLen];
// Read stream into byte array.
InpStream.Read(strArr, 0, strLen);
var strmContents = Convert.ToBase64String(strArr);
return strmContents;
}
public static MemoryStream Base64StringToStream(string inputStr)
{
var myByte = Convert.FromBase64String(inputStr);
var theMemStream = new MemoryStream();
theMemStream.Write(myByte, 0, myByte.Length);
return theMemStream;
}
public static void SaveBase64ToFile(string FileMapPath, string strBase64)
{
// Save the image to a file.
var mem = Base64StringToStream(strBase64);
FileStream outStream = File.OpenWrite(FileMapPath);
mem.WriteTo(outStream);
outStream.Flush();
outStream.Close();
}
public static string GetBase64FromFile(string fileMapPath)
{
byte[] imageArray = System.IO.File.ReadAllBytes(fileMapPath);
return Convert.ToBase64String(imageArray);
}
public static string ReplaceFileExt(string fileName, string newExt)
{
var strOut = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + newExt;
return strOut;
}
public static string RemoveInvalidFileChars(string Str)
{
// This regex will include illegal chars you never dreamed of
string illegalCharsPattern = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(illegalCharsPattern)));
return r.Replace(Str, "");
}
public static void AppendToLog(string logMapPathFolder, string logName, string logMessage)
{
var dstring = DateTime.Now.ToString("yyyy-MM-dd");
var logfilename = logMapPathFolder.TrimEnd('\\') + "\\" + dstring + "_" + Path.GetFileNameWithoutExtension(logName) + ".txt";
if (!File.Exists(logfilename)) SaveFile(logfilename, "START" + Environment.NewLine);
using (StreamWriter w = File.AppendText(logfilename))
{
Log(DateTime.Now.ToString("d/MM/yyyy HH:mm:ss") + " : " + logMessage, w);
}
}
public static void Log(string logMessage, TextWriter w)
{
w.WriteLine($"{logMessage}");
}
public static bool CompareAreSame(string fileMapPath1, string fileMapPath2)
{
byte[] file1 = File.ReadAllBytes(fileMapPath1);
byte[] file2 = File.ReadAllBytes(fileMapPath2);
if (file1.Length == file2.Length)
{
for (int i = 0; i < file1.Length; i++)
{
if (file1[i] != file2[i])
{
return false;
}
}
return true;
}
return false;
}
}
}