C#统计目录中文件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 filepath=@"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("目前剪贴板中数据不可转换为文本","错误");