發布時間:2020-08-06 15:31 作者:獨孤劍 閱讀:3645
using System; using System.IO; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long length = FileSize("D:\\001.txt"); Console.WriteLine(length); Console.Read(); } /// <summary> /// 獲取, 讀取指定文件大小, 返回單位為字節 /// </summary> /// <param name="path">文件路徑</param> /// <returns></returns> public static long FileSize(string path) { // 空路徑名是不合法的 if (string.IsNullOrEmpty(path)) { throw new ArgumentException("Empty path name is not legal.", "path"); } // 判斷路徑是否存在 if (!File.Exists(path)) { throw new FileNotFoundException("Could not find file '" + path + "'.", path); } using (System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { return fileStream.Length; } } } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對您有幫助,歡迎給作者打賞