Search code examples
.netfilefile-iofileinfo

What is the fastest way of getting a file length in .net?


What is the fastest way of getting a file length in .net?

Note: I am accessing files via a network share.

So far I have

  • 1.5ms FileInfo.Length
  • .5ms FileStream().Length

Solution

  • Derived from Adi_aks answer

    public static long GetFileLength(string path)
    {
        using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            return fileStream.Length;
        }
    }