1 void CGetFileSizeDlg::OnBnClickedButton1()
2 {
3 CString strFile;
4 GetDlgItemText(IDC_EDIT1, strFile);
5 DWORD dwHigh = 0;//(文件大小超过4GB)存放33-64位高位
6 DWORD dwLow = 0;//存放1-32位低位
7 HANDLE hFile = CreateFile(strFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
8 if (hFile == INVALID_HANDLE_VALUE) {
9 DWORD errCode = GetLastError();
10 CString szErr;
11 szErr.Format(_T("CreateFile出错啦!错误代码是%d"), errCode);
12 MessageBox(szErr);
13 CloseHandle(hFile);
14 return;
15 }
16 dwLow = GetFileSize(hFile, &dwHigh);
17 LONGLONG lTotal = ((LONGLONG)dwHigh << 32) | dwLow;//形成最终8字节的文件大小LONGLONG(INT64)型数
18 DOUBLE dbTotal = (DOUBLE)lTotal;
19 DOUBLE kbSize = dbTotal / 1024;
20 DOUBLE mbSize = dbTotal / (1024*1024);
21 DOUBLE gbSize = dbTotal / (1024*1024*1024);
22 CString cbSize;
23 cbSize.Format(_T("文件大小是%lld字节(%0.2fKB,%0.2fMB,%0.2fGB)"), lTotal,kbSize,mbSize,gbSize);
24 MessageBox(cbSize);
25 CloseHandle(hFile);
26 return;
27 }