선언:

C#

[DllImport("kernel32")]
public static extern UInt32 GetFileSize(IntPtr hFile, out UInt32 lpFileSizeHigh);


VB.NET

<DllImport("kernel32")> _
Public Shared Function GetFileSize(hFile As IntPtr, ByRef lpFileSizeHigh As UInt32) As UInt32
End Function




사용 예제:

C#

using System;
using System.Runtime.InteropServices;
namespace ApiReference {
    class ApiExample {
        public struct OFSTRUCT {
            public Byte cBytes;
            public Byte fFixedDisk;
            public UInt16 nErrCode;
            public UInt16 Reserved1;
            public UInt16 Reserved2;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public String szPathName;
        }
        [DllImport("kernel32")]
        public static extern IntPtr OpenFile(String lpFileName, ref OFSTRUCT lpReOpenBuff, UInt32 uStyle);
        [DllImport("kernel32")]
        public static extern UInt32 GetFileSize(IntPtr hFile, out UInt32 lpFileSizeHigh);
        [DllImport("kernel32")]
        public static extern void CloseHandle(IntPtr hObject);
        
        public static void Main(string[] args) {
            
            // 파일을 열기 위해서 구조체를 초기화한다.
            OFSTRUCT ofs = default(OFSTRUCT);
            ofs.cBytes = (Byte) Marshal.SizeOf(typeof(OFSTRUCT));
            
            // 파일을 열고
            IntPtr hFile = OpenFile(@"C:\windows\explorer.exe", ref ofs, 0);
            
            // 파일의 크기를 가져온다.
            UInt32 dwHigh;
            UInt32 dwFileSize = GetFileSize(hFile, out dwHigh);
            
            // 마지막으로 파일을 닫는다.
            CloseHandle(hFile);
            Console.WriteLine("파일 위치: {0}", ofs.szPathName);
            Console.WriteLine("파일 크기: {0:N0} Bytes", dwFileSize);
            
            Console.ReadKey(true);
        }
    }
}


VB.NET

Imports System
Imports System.Runtime.InteropServices
Namespace ApiReference
    Class ApiExample
        Public Structure OFSTRUCT
            Public cBytes As [Byte]
            Public fFixedDisk As [Byte]
            Public nErrCode As UInt16
            Public Reserved1 As UInt16
            Public Reserved2 As UInt16
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst := 128)> _
            Public szPathName As [String]
        End Structure
        <DllImport("kernel32")> _
        Public Shared Function OpenFile(lpFileName As [String], ByRef lpReOpenBuff As OFSTRUCT, uStyle As UInt32) As IntPtr
        End Function
        <DllImport("kernel32")> _
        Public Shared Function GetFileSize(hFile As IntPtr, ByRef lpFileSizeHigh As UInt32) As UInt32
        End Function
        <DllImport("kernel32")> _
        Public Shared Sub CloseHandle(hObject As IntPtr)
        End Sub

        Public Shared Sub Main(args As String())

            ' 파일을 열기 위해서 구조체를 초기화한다.
            Dim ofs As OFSTRUCT = Nothing
            ofs.cBytes = CType(Marshal.SizeOf(GetType(OFSTRUCT)), [Byte])

            ' 파일을 열고
            Dim hFile As IntPtr = OpenFile("C:\windows\explorer.exe", ofs, 0)

            ' 파일의 크기를 가져온다.
            Dim dwHigh As UInt32
            Dim dwFileSize As UInt32 = GetFileSize(hFile, dwHigh)

            ' 마지막으로 파일을 닫는다.
            CloseHandle(hFile)
            Console.WriteLine("파일 위치: {0}", ofs.szPathName)
            Console.WriteLine("파일 크기: {0:N0} Bytes", dwFileSize)

            Console.ReadKey(True)
        End Sub
    End Class
End Namespace




예제 실행 결과:




매개 변수 설명:

hFile - 크기를 가져올 파일의 핸들을 입력합니다.

lpFileSizeHigh - 더블워드의 크기를 받아올 변수를 입력합니다. 자세한 내용은 비고를 참고하시기 바랍니다.




API 설명:

파일의 크기를 가져옵니다.




참고:

GetFileSize (MSDN)




요구 사항:

Windows XP 이상




비고:

lpFileSizeHigh 매개 변수의 경우 생략이 가능합니다. C#에서 lpFileSizeHigh 매개 변수를 생략하고자 하는 경우 API의 선언을 out UInt32 lpFileSizeHigh 가 아닌 UInt32 lpFileSizeHigh 로 변경해주시면 됩니다.



'API Reference' 카테고리의 다른 글

48. GetDiskFreeSpace  (0) 2014.12.09
47. EmptyWorkingSet  (1) 2014.11.12
45. OpenFile  (0) 2014.11.09
44. OFSTRUCT  (0) 2014.11.09
43. GetFileAttributes  (0) 2014.11.06

+ Recent posts