forked from yanw/App_win_iot_V2.0
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
6.8 KiB
216 lines
6.8 KiB
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
|
|
{
|
|
/// <summary>
|
|
/// 获取文件MD5 哈希码
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
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 "";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 复制替换
|
|
/// </summary>
|
|
/// <param name="startdir"></param>
|
|
/// <param name="enddir"></param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 删除文件夹
|
|
/// </summary>
|
|
/// <param name="srcPath"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 压缩文件
|
|
/// </summary>
|
|
/// <param name="SrcFile"></param>
|
|
/// <param name="DstFile"></param>
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 截屏
|
|
/// </summary>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 写Json文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="jsonContents"></param>
|
|
public static void WriteJsonFile(string path,string jsonContents)
|
|
{
|
|
File.WriteAllText(path, jsonContents, System.Text.Encoding.UTF8);
|
|
}
|
|
/// <summary>
|
|
/// 读Json文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 读取文本内容
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|