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 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.
精彩评论