Share via


File Operation in the Smartphone

Unlike the Pocket PC, data in the object store of the Smartphone is volatile. When the phone is turned off, the data goes away. Fortunately, there is a way to persistently save data in the file system.

The Smartphone implements an external file system using flash memory internal to the phone. This file system is just like a file system that would appear if a Compact Flash or SD card was inserted into the system, but in this case the file system is not removable. The room in the internal persistent store is limited,, so it's not a great area for storing huge databases or MP3 files. Still, it does provide a place to store application state data or other information. While the method for finding external file systems was discussed in Chapter 8, the Smartphone extends a standard shell call that will return the proper subdirectory in which an application can store its data.

The function SHGetSpecialFolderPath was covered in Chapter 16. The Smartphone adds an additional constant, CSIDL_APPDATA. Using this CSIDL value will return the name of the application data folder that is persistent. An application can then create a subdirectory under the persistent folder where it can store its data. Because the persistent folder is used by all applications, you should be careful to uniquely name your application's directory. The following code demonstrates finding the application data folder and creating a directory.

int CreateAppFolder (HWND hWnd, TCHAR *pszAppFolder, int nMax) {

    const TCHAR szMyAppFolderName[] = TEXT ("ProgWinCESpSample");
    TCHAR szPath[MAX_PATH];

    // It doesn't help to have a path longer than MAX_PATH
    if (nMax > MAX_PATH)
        nMax = MAX_PATH;

    BOOL f = SHGetSpecialFolderPath (hWnd, szPath, CSIDL_APPDATA, FALSE);

    // See if everything will fit in output string
    int nLen = lstrlen (szPath);
    if (nLen + 2 + (int)lstrlen (szMyAppFolderName) > nMax)
        return -2;

    // Copy app folder name to parameter
    lstrcpy (pszAppFolder, szPath);

    // Append directory separator character as needed
    if (szPath[nLen] != TEXT ('\\'))
        lstrcat (pszAppFolder, TEXT("\\"));

    // Append my folder name
    lstrcat (pszAppFolder, szMyAppFolderName);

    // See if directory exists.
    if (GetFileAttributes (pszAppFolder) == 0xffffffff) {
        // See why call failed      
        if (GetLastError () == ERROR_PATH_NOT_FOUND) {
            // Wasn't there, create the directory
            if (!CreateDirectory (pszAppFolder, NULL))
                return -3;
        } else 
            return -4;  // Dir created but unaccessible
    } else
        return 1;       // Indicate directory already exists
    return 0;           // Indicate directory created
}

Data stored in the registry is persistent. The Smartphone takes steps to save and restore the registry when the system shuts down and starts back up.

This topic is from Programming Microsoft Windows CE, Third Edition, by Douglas Boling, published by Microsoft Press. © 2003 by Douglas McConnaughey Boling. Reprinted here by permission of the author.