Skip to content

Instantly share code, notes, and snippets.

@Koki-Shimizu
Created May 28, 2015 19:59
Show Gist options
  • Select an option

  • Save Koki-Shimizu/529e178e2e6ba9f3aa62 to your computer and use it in GitHub Desktop.

Select an option

Save Koki-Shimizu/529e178e2e6ba9f3aa62 to your computer and use it in GitHub Desktop.

Revisions

  1. Koki-Shimizu created this gist May 28, 2015.
    59 changes: 59 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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;
    }
    }
    }
    }