Get file size without using System.IO.FileInfo?

前端 未结 6 1558
[愿得一人]
[愿得一人] 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:26

    Not a direct answer...because I am not sure there is a faster way using the .NET framework.

    Here's the code I am using:

      List list = new List();
      DirectoryInfo di = new DirectoryInfo("C:\\Program Files");
      FileInfo[] fiArray = di.GetFiles("*", SearchOption.AllDirectories);
      foreach (FileInfo f in fiArray)
        list.Add(f.Length);
    

    Running that, it took 2709ms to run on my "Program Files" directory, which was around 22720 files. That's no slouch by any means. Furthermore, when I put *.txt as a filter for the first parameter of the GetFiles method, it cut the time down drastically to 461ms.

    A lot of this will depend on how fast your hard drive is, but I really don't think that FileInfo is killing performance.

    NOTE: I thikn this only valid for .NET 4+

提交回复
热议问题