1. [代码]统计目录中文件MD5值
using System.IO;
using System.Security.Cryptography;
using System.Collections;
using System.Windows.Forms;
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
string filepath=(string)iData.GetData(DataFormats.Text);
StringBuilder sbMD5= new StringBuilder(1024);
string [email protected]"C:\tmp";
DirectoryInfo dir = new DirectoryInfo((filepath.LastIndexOf(Path.DirectorySeparatorChar) == filepath.Length - 1) ? filepath : filepath + Path.DirectorySeparatorChar);
FileSystemInfo[] fileArr = dir.GetFileSystemInfos();
Queue<FileSystemInfo> Folders = new Queue<FileSystemInfo>(dir.GetFileSystemInfos());
while (Folders.Count > 0)
{
FileSystemInfo atom = Folders.Dequeue();
FileInfo f = atom as FileInfo;
if (f == null)
{
DirectoryInfo d = atom as DirectoryInfo;
foreach (FileSystemInfo fi in d.GetFileSystemInfos())
Folders.Enqueue(fi);
}
else
{
FileStream fs = new FileStream(f.FullName,FileMode.Open,FileAccess.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte [] md5byte = md5.ComputeHash(fs);
int i,j;
StringBuilder sb = new StringBuilder(16);
foreach (byte b in md5byte)
{
i = Convert.ToInt32(b);
j = i >> 4;
sb.Append(Convert.ToString(j,16));
j = ((i << 4) & 0x00ff) >> 4;
sb.Append(Convert.ToString(j,16));
}
sbMD5.Append(f.FullName);
sbMD5.Append("\t");
sbMD5.Append(sb.ToString());
sbMD5.Append("\r\n");
}
}
Clipboard.SetText(sbMD5.ToString());
}
else
MessageBox.Show("目前剪贴板中数据不可转换为文本","错误");
2. [代码][C#]代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System.Collections;
using System.Windows.Forms;
http://www.huiyi8.com/gongzuozongjie/?
static void listFiles(string rootDirectory,string searchPattern)
{工作总结
var files=from fileName in Directory.GetFiles(
rootDirectory,searchPattern)
select new
{
Name=fileName,
LastWriteTime=File.GetLastWriteTime(fileName)
};
StringBuilder sbMD5= new StringBuilder(1024);
foreach(var file in files)
{
FileStream fs = new FileStream(file.Name,FileMode.Open,FileAccess.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte [] md5byte = md5.ComputeHash(fs);
StringBuilder sb = new StringBuilder(16);
foreach (byte b in md5byte)
{
int i = Convert.ToInt32(b);
int j = i >> 4;
sb.Append(Convert.ToString(j,16));
j = ((i << 4) & 0x00ff) >> 4;
sb.Append(Convert.ToString(j,16));
}
sbMD5.Append(f.Name);
sbMD5.Append("\t");
sbMD5.Append(f.LastWriteTime);
sbMD5.Append("\t");
sbMD5.Append(sb.ToString());
sbMD5.Append("\r\n");
}
Clipboard.SetText(sbMD5.ToString());
}
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
string filepath=(string)iData.GetData(DataFormats.Text);
listFiles(filepath,"*");
}