Creating and Opening a File or Directory

Other versions of this page are also available for the following:

Windows Mobile Not SupportedWindows Embedded CE Supported

8/28/2008

Call the CreateFile function to create a new file or to open an existing file. When you call CreateFile, Windows Embedded CE searches for a file in the directory that you specify. Depending on the parameters that you pass into CreateFile, Windows Embedded CE 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. You also determine if Windows Embedded CE can share the file. If you choose not to share the file, Windows Embedded CE enables another call to CreateFile on the file only after your application closes the file. After the file is opened or created, Windows Embedded 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. 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 present for backwards compatability 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;
   }
}

See Also

Concepts

Creating, Deleting, and Maintaining Files
File System Operations