开发者

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

开发者 https://www.devze.com 2023-01-17 01:27 出处:网络
What is the fastest way of getting a file length in .net? Note: I am accessing files via a network share.

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

Note: I am accessing files via a network share.

So fa开发者_开发技巧r I have

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


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;
    }
}


long size = File.OpenRead(path).Length;


You could PInvoke the FindFirstFile or GetFileAttributesEx API calls, but that seems like a lot of extra work that the FileInfo class is already doing for you. Otherwise I'm wondering the same thing Scott is: why would you not want to use FileInfo?


Why not just use FileInfo.Length?

You could p/invoke the Win32 APIs: CreateFile, GetFileSizeEx, and CloseHandle if you really wanted to.

0

精彩评论

暂无评论...
验证码 换一张
取 消