Share via


Hard-coding "My Documents"? Fix Your Code

You've probably heard this warning before - don't hard-code paths such as "\My Documents". This path has changed in past versions of Windows Mobile so it is not safe to assume the directory structure. The correct way to obtain a device's My Documents path is using the SHGetSpecialFolderPath API with a CSIDL_PERSONAL parameter. In managed code you can use System.Environment.GetFolderPath with various Environment.SpecialFolder constants.

I'm reemphasizing this because of some information I found out today. We're localizing the My Documents path for various languages in the upcoming version of Windows Mobile (codename "Crossbow"). I was surprised to know that this path isn't already localized, as it should be. After all, "My Documents" doesn't mean much to a Japanese user. I'm not sure if any other folder paths will be localized at this time, but in general it's very bad practice to assume any path (e.g. \Windows, \Storage Card etc.) If you've got code that does this, fix it by using APIs such as SHGetSpecialFolderPath, FindFirstFlashCard and SHGetDocumentsFolder.

You've still got some time before "Crossbow" devices hit the market, but it's never too early to check your code for such defects.

Comments

  • Anonymous
    July 25, 2006
    The comment has been removed

  • Anonymous
    July 25, 2006
    The comment has been removed

  • Anonymous
    July 26, 2006
    The comment has been removed

  • Anonymous
    July 26, 2006
    What's the plan at Microsoft for fixing the DSOD issue that plagues all devices and all owners?

  • Anonymous
    July 26, 2006
    What about a case where the user can change the locale and reboot? Does the device then rename all the SHXxxxx managed folders?

  • Anonymous
    July 27, 2006
    This is an extremely stupid, app-breaking change without any real benefits for anyone. Sure, it's easy to change your application so that it will work in future versions, but what about the old apps that are not developed by anyone any more, and that are still useful for a lot of people. I urge you to reconsider this, as one of the great benefits of Windows Mobile compared to Symbian/Series 60 is the backward compatibility.

    Kim Nyberg

  • Anonymous
    July 28, 2006
    And changing it back to "My Documents" for every language once we finally get MUI for Pocket PC ?!
    I would say stick to "My Documents" and localise only the UI through single-language MUI-style localisation, so that it's ready for the MUI switch.

  • Anonymous
    July 30, 2006
    Here's some clarification:

    Windows desktop and Smartphone platforms have the logical file system path always named "My Documents". The UI is localized using MUI redirection. So as 'dono' pointed out above, the actual folder is named "My Documents", but it appears in a local language to the end-user based on MUI. But again, this is true only on desktop and Smartphone (not PPC).

    Pocket PC does not currently support MUI and as far as I know, there are no plans to add MUI support in the future. So on the PPC, the actual name of the "My Documents" folder will be localized.

    When the user changes the locale from Control Panel, the file names and directory names do not change. On the Smartphone, display names only change when the UI Language is changed.

    I hope this answers some of the questions people have asked above.

    Bottomline, please avoid hard coding the name or path of My Documents and other folders.

    -Mel

  • Anonymous
    August 03, 2006
    The comment has been removed

  • Anonymous
    August 06, 2006
    The comment has been removed

  • Anonymous
    August 07, 2006
    The comment has been removed

  • Anonymous
    April 12, 2008
    Nice to read, as I am currently trying to translate these paths to german on a windows mobile 6.1. That's why I give the sentence back to Microsoft: "Fix Your Code!!!" I found several system files such as shellres.dll.0407.mui tabres.dll.0407.mui and many other with hardcoded "My Documents" path which actually influence system behaviour.

  • Anonymous
    July 14, 2008
    A few blogs I found detailing the API/Object/Library features for getting my documents and other associated...

  • Anonymous
    August 24, 2008
    Enumerating Storage Cards Because a device can have multiple storage cards with various names, your application cannot make any assumptions about the naming or path to a particular card. Pocket PC provides the FindFirstFlashCard and FindNextFlashCard functions to allow programmatic enumeration of storage cards. To determine whether your device has a storage card, call the FindFirstFlashCard and FindNextFlashCard functions. FindFirstFlashCard returns a search handle that FindNextFlashCard uses. It also returns a pointer to the first storage card, if any. FindNextFlashCard returns a pointer to the next storage card and a BOOL value indicating whether the search was successful. The following code example shows how to use FindFirstFlashCard and FindNextFlashCard to locate up to ten storage cards on a Pocket PC device: int index; int iNumOfFlashCard = 0;      // tTotal number of storage                   // cards BOOL bContinue = TRUE;       // If TRUE, continue                  // searching                  // If FALSE, stop searching. TCHAR szRootDirPath[MAX_PATH];   // Root directory path of                  // storage cards. TCHAR szSCName[1000];        // String for storing the                  // storage card name with                  // the full path HANDLE hFlashCard;         // Search handle for storage                  // cards WIN32_FIND_DATA *lpwfdFlashCard;  // Structure for storing                  // card information. WIN32_FIND_DATA *lpwfdFlashCardTmp; // Structure for storing                  // card information                  // temporarily lpwfdFlashCardTmp = (WIN32_FIND_DATA *) LocalAlloc (LPTR, 10 * sizeof (WIN32_FIND_DATA)); if (lpwfdFlashCardTmp == NULL)   // Failed allocate memory return; hFlashCard = FindFirstFlashCard (&lpwfdFlashCardTmp [0]); if (hFlashCard == INVALID_HANDLE_VALUE) {   LocalFree (lpwfdFlashCardTmp);  // Free the memory.   return; } while (bContinue) {   iNumOfFlashCard += 1;   // Search for the next storage card.   bContinue = FindNextFlashCard (hFlashCard, &lpwfdFlashCardTmp        [iNumOfFlashCard]); } FindClose (hFlashCard);          // Close the search handle. if (iNumOfFlashCard > 0) {   // Allocate memory for lpwfdFlashCard.   lpwfdFlashCard = (WIN32_FIND_DATA *) LocalAlloc (LPTR,          iNumOfFlashCard * sizeof (WIN32_FIND_DATA));   if (lpwfdFlashCard == NULL)      // Failed allocate memory    return;   // Assign lpwfdFlashCardTmp to lpwfdFlashCard   for (index=0; index < iNumOfFlashCard; ++index)   {    lpwfdFlashCard [index] = lpwfdFlashCardTmp [index];   }   LocalFree (lpwfdFlashCardTmp);  // Free the memory. }

  • Anonymous
    August 24, 2008
    eonjoy man. if u have any sort of any issue regarding Pocket pc contact PPC guru to solve it.