Creating and Opening a File or Directory

Call the CreateFile function to create a new file or to open an existing file. When you call CreateFile, Windows CE searches for a file in the directory that you specify. Depending on the parameters that you pass into CreateFile, Windows CE either opens the file, creates a new file, or truncates the old file. When you open a file, you set the read/write access for that file. Further, you also determine if Windows CE can share the file. If you choose not to share the file, Windows CE enables another call to CreateFile on the file only after your application closes the file. Once the file is opened or created, Windows CE assigns a unique file handle to the file. An application can use the file handle in functions that read from, write to, or describe the file. Note that files cannot be created with the overlapped attribute set.

Call the CreateDirectory function, with the full path of the new directory in the lpPathName parameter, to create a directory. The lpSecurityAttributes parameter is a holdover from Windows NT, and it should be set to NULL.

The following code example shows how to use CreateFile to open an existing file for reading.

void OpenFileExample (void)
{
  HANDLE hFile;

  hFile = CreateFile (TEXT("\MYFILE.TXT"),    // Open MYFILE.TXT
                      GENERIC_READ,           // Open for reading
                      FILE_SHARE_READ,        // Share for reading
                      NULL,                   // No security
                      OPEN_EXISTING,          // Existing file only
                      FILE_ATTRIBUTE_NORMAL,  // Normal file
                      NULL);                  // No template file

  if (hFile == INVALID_HANDLE_VALUE)
  {
    // Your error-handling code goes here.
    return;
  }
} // End of OpenFileExample code