|
本帖最后由 宇智波·佐助 于 2019-7-5 13:33 编辑
1.首先告诉大家怎么查看文件的MD5码,命令窗口中输入如下命令:
- certutil -hashfile 文件名.后缀 MD5
复制代码
2.原理剖析:
当某个文件的某段数据改变了,哪怕是变了一个字节,那么这个文件的MD5码就会跟着改变,我的做法非常简单,
就是在文件末尾写入一个UUID码,写入UUID码,这样就确保了这个世界上不会存在MD5一样的文件,除非你
把我这个UUID码拿走去生成这个文件.
这种方法理论上支持所有的媒体文件,但不支持文本文件,虽然可以照常运行也可以达到修改MD5的目的,但是你文本内容却已经被我改变了
所以如果你要修改的是文本文件的话,那么这种方法 并不可取,如果是媒体文件,那么没毛病!
完整代码:
- using System;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Reflection;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace UpdFileMD5 {
- public partial class HomeForm : Form {
- private DataTable dt; //表格的数据源
- private bool runing; //是否运行,用于停止运行的参数
- public HomeForm() {
- InitializeComponent();
- Init();
- }
- private void Init() {
- openFileDialog1.Multiselect = true;
- openFileDialog1.Title = "可以多选(框选)";
- //openFileDialog1.Filter = @"(*.jpg,*.png,*.jpeg,*.bmp,*.gif,*.mp4)|*.jgp;*.png;*.jpeg;*.bmp;*.gif;*.mp4|All files(*.*)|*.*";
- //初始化表格
- dt = new DataTable();
- dt.Columns.Add(new DataColumn("文件位置") { DataType = Type.GetType("System.String") });
- dt.Columns.Add(new DataColumn("状态"){DataType = Type.GetType("System.String")});
- dataGridView1.AllowUserToAddRows = false; //解决用户点击最后一行老是弹出新行的问题
- dataGridView1.DataSource = dt;
- DoubleBuffered(dataGridView1, true);
- dataGridView1.Columns[1].FillWeight = 40;
- }
- /// <summary>
- /// 导入文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void importFile_button_Click(object sender, EventArgs e) {
- if (openFileDialog1.ShowDialog() == DialogResult.OK) {
- string[] absPaths = openFileDialog1.FileNames; //绝对路径
- foreach (var absPath in absPaths) {
- DataRow dr = dt.NewRow();
- dr[0] = absPath;
- dr[1] = "";
- dt.Rows.Add(dr);
- }
- }
- }
- /// <summary>
- /// 表格序号
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) {
- Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
- TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle,
- dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
- }
- /// <summary>
- /// 解决行头闪烁问题
- /// </summary>
- /// <param name="dgv"></param>
- /// <param name="setting"></param>
- private new void DoubleBuffered(DataGridView dgv, bool setting) {
- Type dgvType = dgv.GetType();
- PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
- BindingFlags.Instance | BindingFlags.NonPublic);
- pi.SetValue(dgv, setting, null);
- }
- /// <summary>
- /// 开始执行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void start_button_Click(object sender, EventArgs e) {
- if (folderBrowserDialog1.ShowDialog() == DialogResult.OK){
- new Thread(() => {
- try{
- start_button.Invoke(new MethodInvoker(() => { start_button.Enabled = false; }));
- importFile_button.Invoke(new MethodInvoker(() => { importFile_button.Enabled = false; }));
- runing = true;
- string savePath = folderBrowserDialog1.SelectedPath;
- for (int i = 0; i < dt.Rows.Count; i++){
- if (!runing){
- break;
- }
- string absPath = dt.Rows[i][0].ToString();
- if (!File.Exists(absPath)){
- SetLog(i, "文件不存在");
- continue;
- }
- if (!Directory.Exists(savePath)){
- SetLog(i, "导出位置错误");
- continue;
- }
- CopyToNewFile(absPath, savePath + @"\new_" + Path.GetFileName(absPath));
- SetLog(i, "修改成功");
- }
- } catch (Exception e2){
- MessageBox.Show(e2.Message);
- } finally{
- start_button.Invoke(new MethodInvoker(() => { start_button.Enabled = true; }));
- importFile_button.Invoke(new MethodInvoker(() => { importFile_button.Enabled = true; }));
- MessageBox.Show("修改完成");
- }
- }).Start();
- }
- }
- /// <summary>
- /// 设置状态
- /// </summary>
- /// <param name="index"></param>
- /// <param name="msg"></param>
- private void SetLog(int index,string msg) {
- dataGridView1.Invoke(new MethodInvoker(() => {
- dt.Rows[index][1] = msg;
- }));
- }
- /// <summary>
- /// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓关键代码:修改文件MD5码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
- /// </summary>
- /// <param name="source"></param>
- /// <param name="target"></param>
- public void CopyToNewFile(string oldFile, string newFile) {
- //1、创建一个负责读取的流
- using (FileStream fsRead = new FileStream(oldFile, FileMode.Open, FileAccess.Read)) {
- //2、创建一个写入流
- using (FileStream fsWrite = new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write)) {
- //3、多媒体文件较大,循环读取
- byte[] buffer = new byte[1024 * 1024];
- int r = fsRead.Read(buffer, 0, buffer.Length);
- while (r != 0) {
- fsWrite.Write(buffer, 0, r);//写入
- //返回本次读取实际读取到的字节数
- r = fsRead.Read(buffer, 0, buffer.Length);
- }
- //通过往源文件末尾添加uuid二进制数据来改变原文件的MD5值
- buffer = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N"));
- fsWrite.Write(buffer, 0, buffer.Length);//最后一次读取,可能不是5M
- }
- }
- }
- /// <summary>
- /// 停止运行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void stop_button_Click(object sender, EventArgs e) {
- runing = false;
- }
- private void 使用说明ToolStripMenuItem_Click(object sender, EventArgs e) {
- HelpForm hp = new HelpForm();
- hp.ShowDialog();
- }
- private void 关于ToolStripMenuItem1_Click(object sender, EventArgs e) {
- AboutForm af = new AboutForm();
- af.ShowDialog();
- }
- private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e) {
- System.Environment.Exit(0);
- }
- }
- }
复制代码
所有关键业务代码都在这里,置于界面代码,自己用VS拖拽一下就可以了.
哈勃:https://habo.qq.com/file/showdetail?pk=ADcGZl1kB2AIPVs1U2Q%3D
微云:https://share.weiyun.com/5R38yfA
|
|