{"id":794,"date":"2025-04-21T16:58:26","date_gmt":"2025-04-21T08:58:26","guid":{"rendered":"https:\/\/www.hyy.net\/?p=794"},"modified":"2025-04-21T16:58:26","modified_gmt":"2025-04-21T08:58:26","slug":"csharp-fileinfo","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=794","title":{"rendered":"C#\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u5927\u5168"},"content":{"rendered":"<p>C#\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u5927\u5168<\/p>\n<p>\u5728.NET\u5f00\u53d1\u4e2d\uff0c\u7528C#\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u6587\u4ef6\u7684\u5c5e\u6027\u3001\u5927\u5c0f\u3001\u521b\u5efa\u65e5\u671f\u3001\u4fee\u6539\u65e5\u671f\u7b49\u5404\u79cd\u5143\u6570\u636e\u4fe1\u606f\u3002\u6bd4\u5982\u6211\u4eec\u7f51\u7ad9\u9700\u8981\u4e0a\u4f20\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u6269\u5c55\u540d\u79f0\u63a7\u5236\u4e0a\u4f20\u7684\u7c7b\u578b\uff0c\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u6765\u9650\u5236\u4e0a\u4f20\u6587\u4ef6\u7b49\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u7cfb\u5217C#\u4e2d\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u8f7b\u677e\u83b7\u53d6\u6240\u9700\u4fe1\u606f\u5e76\u8fdb\u884c\u6587\u4ef6\u5904\u7406\u3002<\/p>\n<h3>1\u3001\u4f7f\u7528FileInfo\u7c7b\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f<\/h3>\n<p>C#\u4e2d\u63d0\u4f9b\u4e86FileInfo[1]\u7c7b\uff0c\u5b83\u5c01\u88c5\u4e86\u6587\u4ef6\u7684\u4fe1\u606f\u548c\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9e\u4f8b\u5316FileInfo\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u6587\u4ef6\u7684\u5404\u79cd\u4fe1\u606f\u3002<\/p>\n<pre><code>using System;\nusing System.IO;\n\nclass Program\n{\n    static void Main()\n    {\n        string filePath = &quot;C:\\\\Users\\\\username\\\\Documents\\\\example.txt&quot;;\n        FileInfo fileInfo = new FileInfo(filePath);\n\n        Console.WriteLine(&quot;\u6587\u4ef6\u540d\u79f0\uff1a&quot; + fileInfo.Name);\n        Console.WriteLine(&quot;\u6587\u4ef6\u5168\u8def\u5f84\uff1a&quot; + fileInfo.FullName);\n        Console.WriteLine(&quot;\u6587\u4ef6\u5927\u5c0f\uff08\u5b57\u8282\uff09\uff1a&quot; + fileInfo.Length);\n        Console.WriteLine(&quot;\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a&quot; + fileInfo.CreationTime);\n        Console.WriteLine(&quot;\u6587\u4ef6\u6700\u540e\u4fee\u6539\u65f6\u95f4\uff1a&quot; + fileInfo.LastWriteTime);\n        \/\/ \u53ef\u4ee5\u6839\u636e\u9700\u8981\u83b7\u53d6\u66f4\u591a\u5c5e\u6027\n    }\n}<\/code><\/pre>\n<h3>2\u3001\u4f7f\u7528File\u7c7b\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f<\/h3>\n<pre><code>\u9664\u4e86FileInfo\u7c7b\uff0cC#\u4e2d\u7684File[2]\u7c7b\u4e5f\u63d0\u4f9b\u4e86\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u7684\u4e00\u4e9b\u9759\u6001\u65b9\u6cd5\u3002\n\nusing System;\nusing System.IO;\n\nclass Program\n{\n    static void Main()\n    {\n        string filePath = &quot;C:\\\\Users\\\\username\\\\Documents\\\\example.txt&quot;;\n        \/\/ \u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\uff08\u5b57\u8282\uff09\n        long fileSize = new FileInfo(filePath).Length;\n        Console.WriteLine(&quot;\u6587\u4ef6\u5927\u5c0f\uff08\u5b57\u8282\uff09\uff1a&quot; + fileSize);\n        \/\/ \u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\n        DateTime creationTime = File.GetCreationTime(filePath);\n        Console.WriteLine(&quot;\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a&quot; + creationTime);\n        \/\/ \u83b7\u53d6\u6587\u4ef6\u6700\u540e\u4fee\u6539\u65f6\u95f4\n        DateTime lastWriteTime = File.GetLastWriteTime(filePath);\n        Console.WriteLine(&quot;\u6587\u4ef6\u6700\u540e\u4fee\u6539\u65f6\u95f4\uff1a&quot; + lastWriteTime);\n\n        \/\/ \u83b7\u53d6\u6587\u4ef6\u5c5e\u6027\n        FileAttributes attributes = File.GetAttributes(filePath);\n        Console.WriteLine(&quot;\u6587\u4ef6\u5c5e\u6027\uff1a&quot; + attributes);\n    }\n}<\/code><\/pre>\n<h3>3\u3001\u4f7f\u7528Directory\u7c7b\u83b7\u53d6\u76ee\u5f55\u4fe1\u606f<\/h3>\n<p>\u5982\u679c\u9700\u8981\u83b7\u53d6\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528Directory[3]\u7c7b\u3002<\/p>\n<pre><code>using System;\nusing System.IO;\n\nclass Program\n{\n    static void Main()\n    {\n        string directoryPath = &quot;C:\\\\Users\\\\username\\\\Documents&quot;;\n        \/\/ \u83b7\u53d6\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5217\u8868\n        string[] files = Directory.GetFiles(directoryPath);\n        foreach (string file in files)\n        {\n            Console.WriteLine(&quot;\u6587\u4ef6\u540d\uff1a&quot; + Path.GetFileName(file));\n            Console.WriteLine(&quot;\u6587\u4ef6\u5927\u5c0f\uff08\u5b57\u8282\uff09\uff1a&quot; + new FileInfo(file).Length);\n            Console.WriteLine(&quot;\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a&quot; + File.GetCreationTime(file));\n        }\n    }\n}<\/code><\/pre>\n<h3>4\u3001\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\u548c\u7c7b\u578b<\/h3>\n<p>\u5982\u679c\u4f60\u8981\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\u6216\u7c7b\u578b\uff08MIME\u7c7b\u578b\uff09\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528Path[4]\u7c7b\u7684\u4e00\u4e9b\u9759\u6001\u65b9\u6cd5\u3002<\/p>\n<pre><code>using System;\nusing System.IO;\n\nclass Program\n{\n    static void Main()\n    {\n        string filePath = &quot;C:\\\\Users\\\\username\\\\Documents\\\\example.txt&quot;;\n        \/\/\u83b7\u53d6\u6587\u4ef6\u7684\u5168\u8def\u5f84\n        Console.WriteLine(&quot;\u83b7\u53d6\u6587\u4ef6\u7684\u5168\u8def\u5f84\uff1a&quot; + Path.GetFullPath(filePath););\n        \/\/\u83b7\u53d6\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\n        Console.WriteLine(&quot;\u83b7\u53d6\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff1a&quot; + Path.GetDirectoryName(filePath));\n        \/\/\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u542b\u6709\u540e\u7f00  example.txt\n        Console.WriteLine(&quot;\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u542b\u6709\u540e\u7f00\uff1a&quot; + Path.GetFileName(filePath));\n         \/\/\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u6ca1\u6709\u540e\u7f00 example\n        Console.WriteLine(&quot;\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u6ca1\u6709\u540e\u7f00\uff1a&quot; + Path.GetFileNameWithoutExtension(filePath));\n        \/\/\u83b7\u53d6\u8def\u5f84\u7684\u540e\u7f00\u6269\u5c55\u540d\u79f0\uff08\u5305\u542b\u70b9\u53f7\uff09  .txt\n        Console.WriteLine(&quot;\u83b7\u53d6\u8def\u5f84\u7684\u540e\u7f00\u6269\u5c55\u540d\u79f0\uff1a&quot; + Path.GetExtension(filePath));\n        \/\/\u83b7\u53d6\u8def\u5f84\u7684\u6839\u76ee\u5f55 C:\\\n        Console.WriteLine(&quot;\u83b7\u53d6\u8def\u5f84\u7684\u6839\u76ee\u5f55\uff1a&quot; + Path.GetPathRoot(filePath));\n\n        \/\/ \u83b7\u53d6\u6587\u4ef6\u7c7b\u578b\uff08MIME\u7c7b\u578b\uff09\n        string mimeType = &quot;application\/unknown&quot;; \/\/ \u9ed8\u8ba4\u503c\n        if (!string.IsNullOrEmpty(extension))\n        {\n            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension);\n            if (regKey != null &amp;&amp; regKey.GetValue(&quot;Content Type&quot;) != null)\n            {\n                mimeType = regKey.GetValue(&quot;Content Type&quot;).ToString();\n            }\n        }\n        Console.WriteLine(&quot;\u6587\u4ef6\u7c7b\u578b\uff1a&quot; + mimeType);\n    }\n\n}<\/code><\/pre>\n<h3>5\u3001\u7ed3\u8bed<\/h3>\n<p>\u65e0\u8bba\u662f\u4f7f\u7528FileInfo\u7c7b\u3001File\u7c7b\u3001Directory\u7c7b\u8fd8\u662fPath\u7c7b\uff0cC#\u63d0\u4f9b\u4e86\u591a\u79cd\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u5c5e\u6027\u3001\u5927\u5c0f\u3001\u65f6\u95f4\u7b49\u4fe1\u606f\u3002\u66f4\u591a\u4fe1\u606f\u5927\u5bb6\u53ef\u4ee5\u53c2\u8003\u5b98\u7f51\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728C#\u6587\u4ef6\u5904\u7406\u4e2d\u6709\u6240\u5e2e\u52a9\uff0c\u6b22\u8fce\u7559\u8a00\u6216\u8005\u5410\u69fd\u3002<\/p>\n<p>\u53c2\u8003\uff1a<br \/>\n[1] <a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.fileinfo\">https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.fileinfo<\/a><br \/>\n[2]<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.file\">https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.file<\/a><br \/>\n[3]<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.directory\">https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.directory<\/a><br \/>\n[4]<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.path\">https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.io.path<\/a><\/p>\n<p><a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzI2NDE1MDE1MQ==&amp;mid=2650851338&amp;idx=1&amp;sn=3aa89d0ad0b0a89b083dc7fc30a7d074&amp;chksm=f14565e2c632ecf47c7fd358187bee7b3e1477659ff7a199e385e0e66ad4de71a0c6fb06aed0&amp;cur_album_id=1924294891300290563&amp;scene=190#rd\">https:\/\/mp.weixin.qq.com\/s?__biz=MzI2NDE1MDE1MQ==&mid=2650851338&idx=1&sn=3aa89d0ad0b0a89b083dc7fc30a7d074&chksm=f14565e2c632ecf47c7fd358187bee7b3e1477659ff7a199e385e0e66ad4de71a0c6fb06aed0&cur_album_id=1924294891300290563&scene=190#rd<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u5927\u5168 \u5728.NET\u5f00\u53d1\u4e2d\uff0c\u7528C#\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u6587\u4ef6\u7684\u5c5e\u6027\u3001\u5927\u5c0f\u3001\u521b\u5efa\u65e5\u671f\u3001\u4fee\u6539\u65e5\u671f\u7b49\u5404\u79cd\u5143\u6570\u636e\u4fe1\u606f\u3002\u6bd4\u5982\u6211\u4eec\u7f51\u7ad9\u9700\u8981\u4e0a\u4f20\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u6269\u5c55\u540d\u79f0\u63a7\u5236\u4e0a\u4f20\u7684\u7c7b\u578b\uff0c\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u6765\u9650\u5236\u4e0a\u4f20\u6587\u4ef6\u7b49\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u7cfb\u5217C#\u4e2d\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u8f7b\u677e\u83b7\u53d6\u6240\u9700\u4fe1\u606f\u5e76\u8fdb\u884c\u6587\u4ef6\u5904\u7406\u3002 1\u3001\u4f7f\u7528FileInfo\u7c7b\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f C#\u4e2d\u63d0\u4f9b\u4e86FileInfo[1]\u7c7b\uff0c\u5b83\u5c01\u88c5\u4e86\u6587\u4ef6\u7684\u4fe1\u606f\u548c\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9e\u4f8b\u5316FileInfo\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u6587\u4ef6\u7684\u5404\u79cd\u4fe1\u606f\u3002 using System; using System.IO; class Program { static void Main() { string filePath = &quot;C:\\\\Users\\\\username\\\\Documents\\\\example.txt&quot;; FileInfo fileInfo = new FileInfo(filePath); Console.WriteLine(&quot;\u6587\u4ef6\u540d\u79f0\uff1a&quot; + fileInfo.Name); Console.WriteLine(&quot;\u6587\u4ef6\u5168\u8def\u5f84\uff1a&quot; + fileInfo.FullName); Console.WriteLine(&quot;\u6587\u4ef6\u5927\u5c0f\uff08\u5b57\u8282\uff09\uff1a&quot; + fileInfo.Length); Console.WriteLine(&quot;\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a&quot; + fileInfo.CreationTime); Console.WriteLine(&quot;\u6587\u4ef6\u6700\u540e\u4fee\u6539\u65f6\u95f4\uff1a&quot; + fileInfo.LastWriteTime); \/\/ \u53ef\u4ee5\u6839\u636e\u9700\u8981\u83b7\u53d6\u66f4\u591a\u5c5e\u6027 } } 2\u3001\u4f7f\u7528File\u7c7b\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f \u9664\u4e86FileInfo\u7c7b\uff0cC#\u4e2d\u7684File[2]\u7c7b\u4e5f\u63d0\u4f9b\u4e86\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\u7684\u4e00\u4e9b\u9759\u6001\u65b9\u6cd5\u3002 using System; using System.IO; class Program { static void Main() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-794","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=794"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/794\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}