{"id":299355,"date":"2024-05-20T17:15:34","date_gmt":"2024-05-20T09:15:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/299355.html"},"modified":"2024-05-20T17:15:45","modified_gmt":"2024-05-20T09:15:45","slug":"c%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83%e4%b8%a4%e4%b8%aa%e6%96%87%e4%bb%b6%e6%98%af%e4%b8%8d%e6%98%af%e5%90%8c%e4%b8%80%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/299355.html","title":{"rendered":"C#\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u662f\u4e0d\u662f\u540c\u4e00\u4e2a"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27151438\/83fdecb2-ea71-44fc-9507-5d9690a78831.webp\" alt=\"C#\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u662f\u4e0d\u662f\u540c\u4e00\u4e2a\" \/><\/p>\n<p><p>\u5728C#\u4e2d\uff0c\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u6bd4\u8f83\u3001\u6587\u4ef6\u5927\u5c0f\u6bd4\u8f83\u3001\u4ee5\u53ca\u9010\u5b57\u8282\u6bd4\u8f83<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u6bd4\u8f83\u662f\u4e00\u79cd\u65e2\u9ad8\u6548\u53c8\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u5e76\u5bf9\u6bd4\u8fd9\u4e9b\u503c\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u786e\u5b9a\u8fd9\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u5b8c\u5168\u76f8\u540c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u5173\u952e\u5728\u4e8e\uff0c\u5373\u4f7f\u662f\u975e\u5e38\u5c0f\u7684\u6587\u4ef6\u53d8\u52a8\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u54c8\u5e0c\u503c\u7684\u663e\u8457\u53d8\u5316\uff0c\u56e0\u6b64\u901a\u8fc7\u54c8\u5e0c\u503c\u5bf9\u6bd4\u53ef\u4ee5\u9ad8\u5ea6\u786e\u4fe1\u6587\u4ef6\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u6bd4\u8f83<\/strong><\/h2>\n<p><p>\u5728C#\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>System.Security.Cryptography<\/code>\u547d\u540d\u7a7a\u95f4\u4e0b\u7684\u54c8\u5e0c\u7b97\u6cd5\uff08\u5982MD5\u3001SHA1\u3001SHA256\u7b49\uff09\u6765\u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u3002\u9996\u5148\uff0c\u9700\u8981\u5206\u522b\u8bfb\u53d6\u4e24\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528\u76f8\u540c\u7684\u54c8\u5e0c\u7b97\u6cd5\u8ba1\u7b97\u51fa\u4e24\u4e2a\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u6700\u540e\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u54c8\u5e0c\u503c\u662f\u5426\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h2>\u8ba1\u7b97\u6587\u4ef6\u54c8\u5e0c\u503c<\/h2>\n<\/p>\n<p><p>\u8981\u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528\u5176<code>ComputeHash<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u6d41\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">using System;<\/p>\n<p>using System.IO;<\/p>\n<p>using System.Security.Cryptography;<\/p>\n<p>public static string GetFileHash(string filePath)<\/p>\n<p>{<\/p>\n<p>    using (var hashAlgorithm = SHA256.Create())<\/p>\n<p>    {<\/p>\n<p>        using (var stream = File.OpenRead(filePath))<\/p>\n<p>        {<\/p>\n<p>            var hash = hashAlgorithm.ComputeHash(stream);<\/p>\n<p>            return BitConverter.ToString(hash).Replace(&quot;-&quot;, &quot;&quot;).ToLowerInvariant();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6bd4\u8f83\u54c8\u5e0c\u503c<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6\u83b7\u5f97\u4e24\u4e2a\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u5c31\u53ef\u4ee5\u7b80\u5355\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u54c8\u5e0c\u503c\u76f8\u540c\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u4e24\u4e2a\u6587\u4ef6\u4e5f\u76f8\u540c\uff1b\u5982\u679c\u4e0d\u540c\uff0c\u5219\u6587\u4ef6\u80af\u5b9a\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">public static bool CompareFilesByHash(string filePath1, string filePath2)<\/p>\n<p>{<\/p>\n<p>    string fileHash1 = GetFileHash(filePath1);<\/p>\n<p>    string fileHash2 = GetFileHash(filePath2);<\/p>\n<p>    return fileHash1 == fileHash2;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u901a\u8fc7\u6587\u4ef6\u5927\u5c0f\u6bd4\u8f83<\/strong><\/h2>\n<p><p>\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\u662f\u5224\u65ad\u5b83\u4eec\u662f\u5426\u76f8\u540c\u7684\u4e00\u79cd\u66f4\u7b80\u5355\u4f46\u4e0d\u592a\u51c6\u786e\u7684\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u83b7\u53d6\u4e24\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u5982\u679c\u5927\u5c0f\u4e0d\u540c\uff0c\u5219\u53ef\u4ee5\u7acb\u5373\u786e\u5b9a\u6587\u4ef6\u4e0d\u540c\uff1b\u5982\u679c\u5927\u5c0f\u76f8\u540c\uff0c\u8fd8\u9700\u8981\u8fdb\u4e00\u6b65\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h2>\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>FileInfo<\/code>\u7c7b\u7684<code>Length<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u8be5\u5c5e\u6027\u8fd4\u56de\u6587\u4ef6\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">using System.IO;<\/p>\n<p>public static long GetFileSize(string filePath)<\/p>\n<p>{<\/p>\n<p>    FileInfo fileInfo = new FileInfo(filePath);<\/p>\n<p>    return fileInfo.Length;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6bd4\u8f83\u6587\u4ef6\u5927\u5c0f<\/h2>\n<\/p>\n<p><p>\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u5982\u679c\u5b83\u4eec\u7684\u5927\u5c0f\u5b8c\u5168\u4e00\u81f4\uff0c\u5219\u53ef\u80fd\u662f\u76f8\u540c\u7684\u6587\u4ef6\uff0c\u4f46\u4ecd\u9700\u8fdb\u4e00\u6b65\u786e\u8ba4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">public static bool CompareFilesBySize(string filePath1, string filePath2)<\/p>\n<p>{<\/p>\n<p>    long fileSize1 = GetFileSize(filePath1);<\/p>\n<p>    long fileSize2 = GetFileSize(filePath2);<\/p>\n<p>    return fileSize1 == fileSize2;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u9010\u5b57\u8282\u6bd4\u8f83<\/strong><\/h2>\n<p><p>\u5982\u679c\u9700\u8981\u7edd\u5bf9\u7684\u51c6\u786e\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u9010\u5b57\u8282\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u6765\u51b3\u5b9a\u5b83\u4eec\u662f\u5426\u5b8c\u5168\u76f8\u540c\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u51c6\u786e\uff0c\u4f46\u5728\u6bd4\u8f83\u5927\u6587\u4ef6\u65f6\u53ef\u80fd\u4f1a\u76f8\u5f53\u8017\u65f6\u3002<\/p>\n<\/p>\n<p><h2>\u9010\u5b57\u8282\u8bfb\u53d6\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u9010\u5b57\u8282\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u9700\u8981\u6253\u5f00\u8fd9\u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u9010\u4e2a\u5b57\u8282\u5730\u8bfb\u53d6\u6bd4\u8f83\uff0c\u76f4\u5230\u627e\u5230\u4e0d\u540c\u7684\u5b57\u8282\u6216\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">using System.IO;<\/p>\n<p>public static bool CompareFilesByByte(string filePath1, string filePath2)<\/p>\n<p>{<\/p>\n<p>    using (FileStream fs1 = File.OpenRead(filePath1), fs2 = File.OpenRead(filePath2))<\/p>\n<p>    {<\/p>\n<p>        if (fs1.Length != fs2.Length)<\/p>\n<p>        {<\/p>\n<p>            return false;<\/p>\n<p>        }<\/p>\n<p>        int byte1, byte2;<\/p>\n<p>        do<\/p>\n<p>        {<\/p>\n<p>            byte1 = fs1.ReadByte();<\/p>\n<p>            byte2 = fs2.ReadByte();<\/p>\n<p>        } while ((byte1 == byte2) &amp;&amp; (byte1 != -1));<\/p>\n<p>        return byte1 == byte2;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5206\u6790\u9010\u5b57\u8282\u6bd4\u8f83\u7684\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u9010\u5b57\u8282\u6bd4\u8f83\u662f\u4e00\u79cd\u975e\u5e38\u7cbe\u786e\u7684\u6bd4\u8f83\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u4fdd\u8bc1\u5728\u5b57\u8282\u7ea7\u522b\u4e0a\u786e\u8ba4\u6587\u4ef6\u662f\u5426\u5b8c\u5168\u76f8\u540c\u3002\u7136\u800c\uff0c\u6b64\u65b9\u6cd5\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u4f1a\u6d88\u8017\u66f4\u591a\u7684\u65f6\u95f4\u548c\u7cfb\u7edf\u8d44\u6e90\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u9700\u8981\u8003\u8651\u6027\u80fd\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u4f1a\u7ed3\u5408\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\u6765\u51b3\u5b9a\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\u3002\u53ef\u4ee5\u9996\u5148\u901a\u8fc7\u6587\u4ef6\u5927\u5c0f\u6765\u8fdb\u884c\u521d\u6b65\u5224\u65ad\uff0c\u5982\u679c\u5927\u5c0f\u76f8\u540c\uff0c\u518d\u4f7f\u7528\u54c8\u5e0c\u503c\u6bd4\u8f83\u63d0\u9ad8\u6548\u7387\uff0c\u6700\u540e\u5728\u5fc5\u8981\u65f6\u91c7\u7528\u9010\u5b57\u8282\u6bd4\u8f83\u4ee5\u786e\u4fdd\u51c6\u786e\u6027\u3002\u8fd9\u6837\u65e2\u4fdd\u8bc1\u4e86\u6bd4\u8f83\u7684\u51c6\u786e\u6027\uff0c\u53c8\u517c\u987e\u4e86\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1aC#\u4e2d\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\uff1f<\/strong><\/p>\n<p>\u7b54\uff1a\u8981\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528C#\u4e2d\u7684\u6587\u4ef6\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u5982\u679c\u4e24\u4e2a\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u76f8\u540c\uff0c\u5219\u53ef\u4ee5\u5224\u65ad\u8fd9\u4e24\u4e2a\u6587\u4ef6\u5185\u5bb9\u662f\u76f8\u540c\u7684\u3002\u53ef\u4ee5\u4f7f\u7528MD5\u3001SHA1\u7b49\u7b97\u6cd5\u6765\u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<p><strong>\u95ee\u98982\uff1aC#\u4e2d\u5982\u4f55\u5b9e\u73b0\u6587\u4ef6\u5185\u5bb9\u7684\u6bd4\u8f83\uff1f<\/strong><\/p>\n<p>\u7b54\uff1a\u8981\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528C#\u4e2d\u7684\u6587\u4ef6\u6d41\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u9010\u4e2a\u5b57\u8282\u8fdb\u884c\u6bd4\u8f83\u3002\u9996\u5148\uff0c\u6253\u5f00\u4e24\u4e2a\u6587\u4ef6\u7684\u6587\u4ef6\u6d41\uff0c\u7136\u540e\u8bfb\u53d6\u6587\u4ef6\u7684\u5b57\u8282\uff0c\u6bd4\u8f83\u5b57\u8282\u7684\u503c\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u8bfb\u53d6\u5230\u7684\u5b57\u8282\u76f8\u540c\uff0c\u5e76\u4e14\u8bfb\u53d6\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u5373\u53ef\u5224\u65ad\u6587\u4ef6\u5185\u5bb9\u76f8\u540c\u3002<\/p>\n<p><strong>\u95ee\u98983\uff1aC#\u4e2d\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\u662f\u5426\u76f8\u540c\uff1f<\/strong><\/p>\n<p>\u7b54\uff1a\u8981\u5224\u65ad\u4e24\u4e2a\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\u662f\u5426\u76f8\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528C#\u4e2d\u7684FileInfo\u7c7b\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\uff0c\u5e76\u8fdb\u884c\u6bd4\u8f83\u3002\u4f7f\u7528FileInfo\u7c7b\u7684LastWriteTime\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\uff0c\u7136\u540e\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\u662f\u5426\u76f8\u540c\u5373\u53ef\u5224\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C#\u4e2d\uff0c\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\u6bd4\u8f83\u3001\u6587\u4ef6\u5927\u5c0f\u6bd4\u8f83\u3001\u4ee5\u53ca\u9010\u5b57\u8282\u6bd4\u8f83\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":299373,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299355"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=299355"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299355\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/299373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=299355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=299355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=299355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}