Get file size without using System.IO.FileInfo?

前端 未结 6 1553
[愿得一人]
[愿得一人] 2021-02-13 15:51

Is it possible to get the size of a file in C# without using System.IO.FileInfo at all?

I know that you can get other things like Name and

6条回答
  •  礼貌的吻别
    2021-02-13 16:06

    From a short test i did, i've found that using a FileStream is just 1 millisecond slower in average than using Pete's GetFileSizeB (took me about 21 milliseconds over a network share...). Personally i prefer staying within the BCL limits whenever i can.

    The code is simple:

    using (var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        return file.Length;
    }
    

提交回复
热议问题