如何批量生成文件的md5校验码
2个回答
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
程序分为两个文件来组织,分别为CheckEntity.cs及frmMain.cs.CheckEntity.cs中包含实体类,md5生成输出类.frmMain.cs主要为操作生成及设置的界面.
源代码分别如下:
CheckEntity.cs文件:
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Windows.Forms;
namespace Md5Check
{
[Serializable]
public class CheckEntity
{
public string FileName { get; set; }
public string Md5Code { get; set; }
public DateTime CreateDate { get; set; }
public DateTime ModifyDate { get; set; }
public string FileFullName { get; set; }
}
[Serializable]
public class CheckActivety
{
public List<CheckEntity> Actives { get; set; }
public DateTime CheckDate { get; set; }
public int FileCnt { get; set; }
}
public static class Helper
{
public static void BinSerialize(object obj, string fileName)
{
using (System.IO.Stream strm = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None))
{
System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
fmt.Serialize(strm, obj);
strm.Flush();
}
}
public static object BinDeserialize(string fileName)
{
object obj = null;
using (System.IO.Stream strm = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None))
{
System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
obj = fmt.Deserialize(strm);
}
return obj;
}
public static string GetMD5Code(string fileName)
{
string hashCode = "";
if (File.Exists(fileName))
{
StreamReader srd = new StreamReader(fileName);
MD5 md = MD5.Create();
byte[] bts = Encoding.UTF32.GetBytes(srd.ReadToEnd());
byte[] code = md.ComputeHash(bts);
for (int i = 0; i < code.Length; i++)
hashCode += code[i].ToString("x");
}
return hashCode;
}
}
public class Check
{
public string ResultFileName { get; set; }
public string RootPath { get; set; }
private string _ExtendNames = ".cs,.asmx,.aspx,.resx,.bat";
public string ExtendNames
{
get { return _ExtendNames; }
set { _ExtendNames = value; }
}
#region Paths
private string[] _CheckPaths;
public string[] CheckPaths
{
get { return _CheckPaths; }
set { _CheckPaths = value; }
}
#endregion
private DateTime BeginTime { get; set; }
private DateTime EndTime { get; set; }
public bool ShowFoldSelector { get; set; }
public bool ShowSavePathSelector { get; set; }
public bool ExpertToTextFile { get; set; }
private List<CheckEntity> _Actives;
public List<CheckEntity> Actives
{
get
{
if (Activity != null)
_Actives = Activity.Actives;
return _Actives;
}
}
public CheckActivety Activity { get; set; }
public Check(bool _ShowFoldSelector, bool _ShowSavePathSelector, bool _ExpertToTextFile)
{
ResultFileName = Application.StartupPath+"\\Check" + DateTime.Now.ToShortDateString() + ".md5";
ShowFoldSelector = _ShowFoldSelector;
ShowSavePathSelector = _ShowSavePathSelector;
ExpertToTextFile = _ExpertToTextFile;
Activity = new CheckActivety();
}
public void Start()
{
if (ShowFoldSelector)
{
//这里可以不必要这样做,以下主要是为了手工指定不连续文件夹方便
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
RootPath = fbd.SelectedPath;
CheckPaths = Directory.GetDirectories(RootPath);
string[] NewCheckPaths = new string[CheckPaths.Length + 1];
NewCheckPaths[0] = fbd.SelectedPath;
Array.Copy(CheckPaths, 0, NewCheckPaths, 1, CheckPaths.Length);
CheckPaths = NewCheckPaths;
}
}
if (ShowSavePathSelector)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "MD5文件(*.md5)|*.md5";
if (sfd.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(sfd.FileName))
ResultFileName = sfd.FileName;
}
/*
ThreadStart startDelegate = new ThreadStart(StartCheck);
Thread thd = new Thread(startDelegate);
thd.Start();
*/
StartCheck();
}
private void StartCheck()
{
try
{
BeginTime = DateTime.Now;
Activity.CheckDate = DateTime.Now;
Activity.FileCnt = 0;
Activity.Actives = new List<CheckEntity>();
for (int i = 0; i < CheckPaths.Length; i++)
{
GetHashValues(Activity, Activity.Actives, CheckPaths[i]);
}
//Serialize the result and save to file
Helper.BinSerialize(Activity, ResultFileName);
//Save Result to text file
if (ExpertToTextFile)
{
StringBuilder sbResult = new StringBuilder();
int j = 1;
foreach (CheckEntity entity in Activity.Actives)
{
sbResult.AppendLine(string.Format("{0,4}. Code:{1,35} <- {2,50} : [{3}]", j, entity.Md5Code.ToUpper(), entity.FileName, entity.FileFullName));
j++;
}
try
{
using (StreamWriter sw = new StreamWriter(ResultFileName + ".txt"))
{
sw.Write(sbResult.ToString());
sw.Flush();
}
}
catch (Exception ex)
{
MessageBox.Show("Save to text file failed! Error Infomation is :" + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
EndTime = DateTime.Now;
MessageBox.Show(string.Format("Computed! Time ellapse: {0} ms Files: {1} ", (EndTime - BeginTime).TotalMilliseconds, Activity.FileCnt), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error has happend when Compute hash values :" + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
源代码分别如下:
CheckEntity.cs文件:
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Windows.Forms;
namespace Md5Check
{
[Serializable]
public class CheckEntity
{
public string FileName { get; set; }
public string Md5Code { get; set; }
public DateTime CreateDate { get; set; }
public DateTime ModifyDate { get; set; }
public string FileFullName { get; set; }
}
[Serializable]
public class CheckActivety
{
public List<CheckEntity> Actives { get; set; }
public DateTime CheckDate { get; set; }
public int FileCnt { get; set; }
}
public static class Helper
{
public static void BinSerialize(object obj, string fileName)
{
using (System.IO.Stream strm = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None))
{
System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
fmt.Serialize(strm, obj);
strm.Flush();
}
}
public static object BinDeserialize(string fileName)
{
object obj = null;
using (System.IO.Stream strm = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None))
{
System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
obj = fmt.Deserialize(strm);
}
return obj;
}
public static string GetMD5Code(string fileName)
{
string hashCode = "";
if (File.Exists(fileName))
{
StreamReader srd = new StreamReader(fileName);
MD5 md = MD5.Create();
byte[] bts = Encoding.UTF32.GetBytes(srd.ReadToEnd());
byte[] code = md.ComputeHash(bts);
for (int i = 0; i < code.Length; i++)
hashCode += code[i].ToString("x");
}
return hashCode;
}
}
public class Check
{
public string ResultFileName { get; set; }
public string RootPath { get; set; }
private string _ExtendNames = ".cs,.asmx,.aspx,.resx,.bat";
public string ExtendNames
{
get { return _ExtendNames; }
set { _ExtendNames = value; }
}
#region Paths
private string[] _CheckPaths;
public string[] CheckPaths
{
get { return _CheckPaths; }
set { _CheckPaths = value; }
}
#endregion
private DateTime BeginTime { get; set; }
private DateTime EndTime { get; set; }
public bool ShowFoldSelector { get; set; }
public bool ShowSavePathSelector { get; set; }
public bool ExpertToTextFile { get; set; }
private List<CheckEntity> _Actives;
public List<CheckEntity> Actives
{
get
{
if (Activity != null)
_Actives = Activity.Actives;
return _Actives;
}
}
public CheckActivety Activity { get; set; }
public Check(bool _ShowFoldSelector, bool _ShowSavePathSelector, bool _ExpertToTextFile)
{
ResultFileName = Application.StartupPath+"\\Check" + DateTime.Now.ToShortDateString() + ".md5";
ShowFoldSelector = _ShowFoldSelector;
ShowSavePathSelector = _ShowSavePathSelector;
ExpertToTextFile = _ExpertToTextFile;
Activity = new CheckActivety();
}
public void Start()
{
if (ShowFoldSelector)
{
//这里可以不必要这样做,以下主要是为了手工指定不连续文件夹方便
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
RootPath = fbd.SelectedPath;
CheckPaths = Directory.GetDirectories(RootPath);
string[] NewCheckPaths = new string[CheckPaths.Length + 1];
NewCheckPaths[0] = fbd.SelectedPath;
Array.Copy(CheckPaths, 0, NewCheckPaths, 1, CheckPaths.Length);
CheckPaths = NewCheckPaths;
}
}
if (ShowSavePathSelector)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "MD5文件(*.md5)|*.md5";
if (sfd.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(sfd.FileName))
ResultFileName = sfd.FileName;
}
/*
ThreadStart startDelegate = new ThreadStart(StartCheck);
Thread thd = new Thread(startDelegate);
thd.Start();
*/
StartCheck();
}
private void StartCheck()
{
try
{
BeginTime = DateTime.Now;
Activity.CheckDate = DateTime.Now;
Activity.FileCnt = 0;
Activity.Actives = new List<CheckEntity>();
for (int i = 0; i < CheckPaths.Length; i++)
{
GetHashValues(Activity, Activity.Actives, CheckPaths[i]);
}
//Serialize the result and save to file
Helper.BinSerialize(Activity, ResultFileName);
//Save Result to text file
if (ExpertToTextFile)
{
StringBuilder sbResult = new StringBuilder();
int j = 1;
foreach (CheckEntity entity in Activity.Actives)
{
sbResult.AppendLine(string.Format("{0,4}. Code:{1,35} <- {2,50} : [{3}]", j, entity.Md5Code.ToUpper(), entity.FileName, entity.FileFullName));
j++;
}
try
{
using (StreamWriter sw = new StreamWriter(ResultFileName + ".txt"))
{
sw.Write(sbResult.ToString());
sw.Flush();
}
}
catch (Exception ex)
{
MessageBox.Show("Save to text file failed! Error Infomation is :" + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
EndTime = DateTime.Now;
MessageBox.Show(string.Format("Computed! Time ellapse: {0} ms Files: {1} ", (EndTime - BeginTime).TotalMilliseconds, Activity.FileCnt), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error has happend when Compute hash values :" + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询