Get file size without using System.IO.FileInfo?

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

    You can try this:

    [DllImport("kernel32.dll")]
    static extern bool GetFileSizeEx(IntPtr hFile, out long lpFileSize);
    

    But that's not much of an improvement...

    Here's the example code taken from pinvoke.net:

    IntPtr handle = CreateFile(
        PathString, 
        GENERIC_READ, 
        FILE_SHARE_READ, 
        0, 
        OPEN_EXISTING, 
        FILE_ATTRIBUTE_READONLY, 
        0); //PInvoked too
    
    if (handle.ToInt32() == -1) 
    {
        return; 
    }
    
    long fileSize;
    bool result = GetFileSizeEx(handle, out fileSize);
    if (!result) 
    {
        return;
    }
    

提交回复
热议问题