How to: List and Locate Storage Card Files

The examples in this topic demonstrate how to list and locate storage card files.

The SHGetDocumentsFolder function works with storage cards that do not have a My Documents folder as well as with older storage cards that do. You should use SHGetDocumentsFolder when accessing all documents on the device, whether they are in main memory or on a storage card.

Example

#include <shlobj.h>
#pragma comment(lib, "ceshell.lib")

TCHAR szPath[MAX_PATH];
if (!SHGetDocumentsFolder(L"\\", szPath))
    MessageBox(NULL, L"Invalid Path", L"Call Failed", MB_OK);
else
    MessageBox(NULL, szPath, L"My Docs Path", MB_OK);

You can also use the EnumProjectsEx and EnumProjectsFilesEx functions to list all folders and files on your storage cards and then use FindFirstProjectFile and FindNextProjectFile to locate particular files, as shown in the following example.

Example

for (index = 0; index < iNumOfFlashCard; ++index)
{
    // Store the directory name in szName.
    wsprintf(szSCName, lpwfdFlashCard[index].cFileName); 
    iNumFolders = EnumProjects (EnumProjProc, lpwfdFlashCard[index].dwOID,
                                PRJ_ENUM_FLASH, 0);
}

    // Note   In the preceding code, EnumProjProc is a user-defined

    // callback function that is used to perform operations within each

    // directory that EnumProjects encounters.

    // The following code shows how to use EnumProjProc to find

    // directories and files on storage cards.

BOOL CALLBACK EnumProjProc (DWORD dwOID, LPARAM lParam)
{
    // Search handle for files under each directory. 
    HANDLE hSearch;    
    // Structure for storing the storage card information 
    // retrieved by CeOidGetInfo.
    CEOIDINFO OidInfo; 
    // Structure for storing file information.
    WIN32_FIND_DATA fd; 
    // String for storing the file or directory 
    // name on every storage card.
    TCHAR szName[1000]; 
    // String for storing the error message.
    TCHAR szErrorMsg[1000];    
    // Wildcard file names with full paths for searching these 
    // types of files.
    TCHAR szFilePath[MAX_PATH]; 
    // Whether the search for files is finished.
    BOOLEAN bFinished = FALSE; 

    // Get storage card information by using its object identifier.
    if (!CeOidGetInfo (dwOID, &OidInfo)) 
    {
        return FALSE; // Stop enumeration.
    }
    // Store the directory name in szName.
    wsprintf(szName, OidInfo.infDirectory.szDirName); 
    // Construct the wildcard file name with the full path for searching 
    // that type of file.
    wcscpy (szFilePath, OidInfo.infDirectory.szDirName);
    wcscat (szFilePath, TEXT("\\*.*"));
    // Search for the first file under the current directory. An 
    // alternative way to do this is to use FindFirstProjectFile.
    hSearch = FindFirstFile( szFilePath, &fd);
    if (hSearch == INVALID_HANDLE_VALUE) 
    { 
        wsprintf(szErrorMsg, TEXT("No files found.")); 
        bFinished = TRUE;      
    } 
    // Find all of the files and retrieve the file names.
    while (!bFinished) 
    { 
        // Store the file name in szName.
        wsprintf(szName, fd.cFileName); 
        if (!FindNextProjectFile(hSearch, &fd)) 
        {
        // Finished.
            bFinished = TRUE;
            if (GetLastError() == ERROR_NO_MORE_FILES) 
            { 
                wsprintf(szErrorMsg, TEXT("Found all of the files.")); 
            } 
            else 
            { 
                wsprintf(szErrorMsg, TEXT("Couldn't find next file.")); 
            } 
        }
    } 
    // Close the search handle. 
    if (!FindClose(hSearch)) 
    { 
        wsprintf(szErrorMsg, TEXT("Couldn't close search handle.")); 
    } 
    return TRUE;
}

See Also

File Management Functions

How to: Enumerate Storage Cards

Send feedback on this topic to the authors.

© 2005 Microsoft Corporation. All rights reserved.