Created
May 28, 2015 19:59
-
-
Save Koki-Shimizu/529e178e2e6ba9f3aa62 to your computer and use it in GitHub Desktop.
.netであるフォルダ以下のファイル毎のハッシュを求める
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Security.Cryptography; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace fciv | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var files = Directory.GetFiles(@"C:\oracle\", "*.sql", SearchOption.AllDirectories).OrderBy(_ => _).ToList(); | |
| var outputXmlName = "result.xml"; | |
| File.Delete(outputXmlName); | |
| var sw = new Stopwatch(); | |
| sw.Start(); | |
| using( var outputXml = File.AppendText(outputXmlName) ) | |
| { | |
| foreach (var file in files) | |
| { | |
| var s = CheckSum(file, 4096); | |
| outputXml.WriteLine(s); | |
| } | |
| outputXml.Flush(); | |
| } | |
| sw.Stop(); | |
| var ts = sw.Elapsed; | |
| Debug.WriteLine(ts); // 出力例:00:00:00.9984668 | |
| } | |
| private static string CheckSum(string file, int bufferSize) | |
| { | |
| try | |
| { | |
| using (var sm = new BufferedStream(File.OpenRead(file), bufferSize)) | |
| { | |
| using( var sha = MD5.Create() ) | |
| { | |
| var checksum = sha.ComputeHash(sm); | |
| return BitConverter.ToString(checksum).Replace("-", String.Empty).ToLower(); | |
| } | |
| } | |
| } | |
| catch | |
| { | |
| // ファイルアクセス拒否時等… | |
| return string.Empty; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment