Вопрос

Hi friends I am working on an application where I should check the file system of the SD card and also I need to check whether the SD card inserted is write protected or not. I was successful in getting the file system details as follows:

TCHAR volumeName[MAX_PATH + 1] = { 0 };
TCHAR fileSystemName[MAX_PATH + 1] = { 0 };
DWORD serialNumber = 0;
DWORD maxComponentLen = 0;
DWORD fileSystemFlags = 0;

LPCWSTR path = deviceData->m_strPath.utf16(); // deviceData gives me the path of the SD Card

// Get the file system details
if (GetVolumeInformation(
                         path,
                         volumeName,
                         ARRAYSIZE(volumeName),
                         &serialNumber,
                         &maxComponentLen,
                         &fileSystemFlags,
                         fileSystemName,
                         ARRAYSIZE(fileSystemName)))
            {
                newData.strFileSystem = QString::fromUtf16(fileSystemName);                
            }

 m_SDInfoList.append(newData); // m_SDInfoList is QList

With this approach I get to know whether the file system is FAT32 or NTFS. Now I want to achieve the write protected details. Is their a Qt API that can gimme whether the inserted SD Card is write protected or not??? Please help :)

Update:

This is what I did using QFileInfo:

QFileInfo fileInfo(deviceData->m_strPath);                    

if(!fileInfo.isWritable())
{                        
       newData.strStatus = "WriteProtect Enabled";
}
else
{
       newData.strStatus = "WriteProtect Disabled";
}

It always ends up giving me WriteProtect Disable even though I have set the write protected permission.

Это было полезно?

Решение

I suspect you can't do that with Qt. You'll need to add custom code for each target platform.

However, you can simply try to create an empty file and then immediately delete it. If creating the file fails then the volume is likely read only (or it has ran out of free space, you'll need to check the error codes to be sure).

Другие советы

Not sure about how to do it in QT, but WinAPI provides you with GetVolumeInformation (http://pinvoke.net/default.aspx/kernel32/GetVolumeInformation.html) method (allows you to check weather drive is write protected or not), that returns the following flag:

/// <summary>
/// The specified volume is read-only.
/// </summary>
ReadOnlyVolume = 0x80000,
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top