简体   繁体   中英

How to know a certain disk's format(is FAT32 or NTFS)

I am programing under windows, c++, mfc How can I know disk's format by path such as "c:\\". Does windows provide such APIs?

The Win32API function ::GetVolumeInformation is what you are looking for.

From MSDN:

GetVolumeInformation Function

BOOL WINAPI GetVolumeInformation(
    __in_opt   LPCTSTR lpRootPathName,
    __out      LPTSTR lpVolumeNameBuffer,
    __in       DWORD nVolumeNameSize,
    __out_opt  LPDWORD lpVolumeSerialNumber,
    __out_opt  LPDWORD lpMaximumComponentLength,
    __out_opt  LPDWORD lpFileSystemFlags,
    __out      LPTSTR lpFileSystemNameBuffer, // Here
    __in       DWORD nFileSystemNameSize
);

Example:

TCHAR fs [MAX_PATH+1];
::GetVolumeInformation(_T("C:\\"), NULL, 0, NULL, NULL, NULL, &fs, MAX_PATH+1);
// Result is in (TCHAR*) fs

Yes it is GetVolumeInformation.

TCHAR szVolumeName[100]    = "";
TCHAR szFileSystemName[10] = "";
DWORD dwSerialNumber       = 0;
DWORD dwMaxFileNameLength  = 0;
DWORD dwFileSystemFlags    = 0;

if(::GetVolumeInformation("c:\\",
                            szVolumeName,
                            sizeof(szVolumeName),
                            &dwSerialNumber,
                            &dwMaxFileNameLength,
                            &dwFileSystemFlags,
                            szFileSystemName,
                            sizeof(szFileSystemName)) == TRUE)
  {
    cout << "Volume name = " << szVolumeName << endl
         << "Serial number = " << dwSerialNumber << endl
         << "Max. filename length = " << dwMaxFileNameLength
         << endl
         << "File system flags = $" << hex << dwFileSystemFlags
         << endl
         << "File system name = " << szFileSystemName << endl;
  }

GetVolumeInformation will give you what you need. It will return the name of the drive format in lpFileSystemNameBuffer.

If you want a nice wrapper around it, you might want to look at Microsoft's CVolumeMaster .

WMI中的Win32_LogicalDisk类具有公开该信息的FileSystem属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM