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 =
"可以多选(框选)"
;
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) {
using
(FileStream fsRead =
new
FileStream(oldFile, FileMode.Open, FileAccess.Read)) {
using
(FileStream fsWrite =
new
FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write)) {
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);
}
buffer = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString(
"N"
));
fsWrite.Write(buffer, 0, buffer.Length);
}
}
}
/// <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);
}
}
}