{"id":4363,"date":"2021-04-24T01:02:57","date_gmt":"2021-04-23T16:02:57","guid":{"rendered":"https:\/\/okamurax.com\/?p=4363"},"modified":"2021-04-24T01:03:03","modified_gmt":"2021-04-23T16:03:03","slug":"c-%e3%83%8f%e3%83%83%e3%82%b7%e3%83%a5%e5%80%a4%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%9f%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e6%af%94%e8%bc%83","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=4363","title":{"rendered":"C# \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u5229\u7528\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u6bd4\u8f03"},"content":{"rendered":"<p>\u7279\u5b9a\u306e\u30d5\u30a9\u30eb\u30c0\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30cf\u30c3\u30b7\u30e5\u3067\u6bd4\u8f03\u3057\u3066\u307f\u308b\u3002\u904e\u53bb\u306b\u4f55\u5ea6\u304b\u30d5\u30a1\u30a4\u30eb\u7834\u640d\u3057\u305f\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u5b9f\u9a13\u3002<\/p>\n<pre class=\"lang:c# decode:true \">using System;\r\nusing System.Linq;\r\nusing System.IO;\r\n\r\nnamespace ConsoleApp1\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            string srcBasePath = @\"C:\\Users\\xxx\\Desktop\\1\";\r\n            string dstBasePath = @\"C:\\Users\\xxx\\Desktop\\2\";\r\n\r\n            foreach(var p in Directory.EnumerateFiles(srcBasePath, \"*\", SearchOption.AllDirectories))\r\n            {\r\n                \/\/ \u9664\u5916\u3059\u308b\u62e1\u5f35\u5b50\u3068\u30d5\u30a1\u30a4\u30eb\u540d\r\n\r\n                if (Path.GetExtension(p) == \".db\") continue;\r\n                if (Path.GetFileName (p) == \"desktop.ini\") continue;\r\n\r\n                string srcHash = FileHash(p);\r\n                string dstHash = FileHash(p.Replace(srcBasePath, dstBasePath));\r\n\r\n                if (srcHash == dstHash)\r\n                {\r\n                    \/\/ \u30cf\u30c3\u30b7\u30e5\u304c\u4e00\u81f4\u3057\u305f\u5834\u5408\u3002\r\n                }\r\n                else if (srcHash != dstHash)\r\n                {\r\n                    \/\/ \u30cf\u30c3\u30b7\u30e5\u304c\u4e00\u81f4\u3057\u306a\u3044\u5834\u5408\u3002\r\n\r\n                    Console.WriteLine(p.Replace(srcBasePath, dstBasePath));\r\n                    Console.WriteLine(dstHash);\r\n                }\r\n            }\r\n            Console.WriteLine(\"\u7d9a\u884c\u3059\u308b\u306b\u306f\u4f55\u304b\u30ad\u30fc\u3092\u62bc\u3057\u3066\u304f\u3060\u3055\u3044\uff0e\uff0e\uff0e\");\r\n            Console.ReadKey();\r\n        }\r\n\r\n        private static string FileHash(string path)\r\n        {\r\n            \/\/ \u5148\u982d\u304b\u3089\u6307\u5b9a\u30d0\u30a4\u30c8\u5206\u3067\u30cf\u30c3\u30b7\u30e5\u3092\u51fa\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u6307\u5b9a\u4ee5\u964d\u306e\u4e0d\u4e00\u81f4\u3067\u306f\u30a8\u30e9\u30fc\u306b\u306a\u3089\u306a\u3044\r\n\r\n            try\r\n            {\r\n                using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))\r\n                {\r\n                    var b = new byte[1024 * 1024]; \/\/ 1MB\r\n                    fs.Read(b, 0, 1024 * 1024);\r\n                    using (var ms = new MemoryStream(b))\r\n                    {\r\n                        var hashProvider = new System.Security.Cryptography.SHA256CryptoServiceProvider();\r\n                        var bs = hashProvider.ComputeHash(ms);\r\n                        return String.Join(\"\", bs.Select(x =&gt; x.ToString(\"x2\")).ToArray()); \/\/ BitConverter\u3068\u7d50\u679c\u306f\u540c\u3058\u3067\u9055\u3046\u66f8\u304d\u65b9\r\n                    }\r\n                }\r\n            }\r\n            catch (Exception)\r\n            {\r\n                return \"\";\r\n            }\r\n\r\n        }\r\n\r\n        private static string FileHash2(string path)\r\n        {\r\n            \/\/ \u30d5\u30a1\u30a4\u30eb\u5168\u3066\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u3001\u30b5\u30a4\u30ba\u304c\u5927\u304d\u3044\u3068\u6642\u9593\u304c\u304b\u304b\u308b\u3002\r\n\r\n            try\r\n            {\r\n                using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))\r\n                {\r\n                    var hashProvider = new System.Security.Cryptography.SHA256CryptoServiceProvider();\r\n                    var bs = hashProvider.ComputeHash(fs);\r\n\r\n                    return BitConverter.ToString(bs).ToLower().Replace(\"-\", \"\");\r\n                }\r\n            }\r\n            catch(Exception e)\r\n            {\r\n                return e.Message;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7279\u5b9a\u306e\u30d5\u30a9\u30eb\u30c0\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30cf\u30c3\u30b7\u30e5\u3067\u6bd4\u8f03\u3057\u3066\u307f\u308b\u3002\u904e\u53bb\u306b\u4f55\u5ea6\u304b\u30d5\u30a1\u30a4\u30eb\u7834\u640d\u3057\u305f\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u5b9f\u9a13\u3002 using System; using System.Linq; using System.IO; namespa &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=4363\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u5229\u7528\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u6bd4\u8f03&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-4363","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4363"}],"version-history":[{"count":2,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4363\/revisions"}],"predecessor-version":[{"id":4365,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4363\/revisions\/4365"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}