using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace IOTContainer.Common { public class FileManage { /// /// 获取文件MD5 哈希码 /// /// /// public static string GetMD5HashFromFile(string fileName) { try { FileStream file = new FileStream(fileName, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch (Exception ex) { Log.MyLog.WriteLogFile($"获取文件哈希值出错fail, error:{ex.Message}"); return ""; } } /// /// 复制替换 /// /// /// public static void CopeReplace(DirectoryInfo startdir, DirectoryInfo enddir) { FileInfo[] files = startdir.GetFiles(); foreach (var item in files) { item.CopyTo(enddir.FullName + "\\" + item.Name, true); } DirectoryInfo[] dirs = startdir.GetDirectories(); foreach (var item in dirs) { string path = enddir.FullName + "\\" + item.Name; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } DirectoryInfo dirn = new DirectoryInfo(path); CopeReplace(item, dirn); } } /// /// 删除文件夹 /// /// /// public static bool DelectDir(string srcPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { try { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } catch (Exception ex) { Log.MyLog.WriteLogFile(ex.ToString(), "FileManageError"); } } return true; } catch (Exception ex) { Log.MyLog.WriteLogFile(ex.ToString(), "FileManageError"); return false; } } /// /// 压缩文件 /// /// /// public static void UnZip(string SrcFile, string DstFile) { try { System.IO.Compression.ZipFile.ExtractToDirectory(SrcFile, DstFile); } catch (Exception ex) { Log.MyLog.WriteLogFile(ex.ToString(), "UnZipError"); } } /// /// 截屏 /// public static string PrintScreen(string imgname) { string pathS = AppDomain.CurrentDomain.BaseDirectory + @"\Screen\"; if (!Directory.Exists(pathS)) { Directory.CreateDirectory(pathS); } string path = AppDomain.CurrentDomain.BaseDirectory + @"\Screen\" + imgname; Bitmap bit = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(bit); g.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), bit.Size); try { if (File.Exists(path)) File.Delete(path); bit.Save(path); } catch (Exception ex) { Log.MyLog.WriteLogFile(ex.ToString() + "----- PrintScreen"); } finally { g.Dispose(); bit.Dispose(); } return path; } /// /// 写Json文件 /// /// /// public static void WriteJsonFile(string path,string jsonContents) { File.WriteAllText(path, jsonContents, System.Text.Encoding.UTF8); } /// /// 读Json文件 /// /// /// public static string ReadJsonFile(string path) { var json = string.Empty; if (File.Exists(path)) { using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, System.IO.FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(fs, Encoding.UTF8)) { json = sr.ReadToEnd(); } } } return json; } /// /// 读取文本内容 /// /// /// public static string ReadContext(string path) { try { FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8); string context = sr.ReadToEnd(); fs.Close(); sr.Close(); sr.Dispose(); fs.Dispose(); return context; } catch (Exception) { return null; } } } }