locked
Unable to create a file in the ApplicationData folder with CreateFile2()

    Question

  • I am not able to get CreateFile2 to create a file in the ApplicationData folder.  Is this possible with CreateFile2()?

        TCHAR filename[_MAX_FNAME]; wcscpy_s(filename, MAX_PATH, ApplicationData::Current->LocalFolder->Path->Data());
        wcscat_s(filename, MAX_PATH, L"\\data\\test.txt");

        fd = CreateFile2(filename,         GENERIC_READ | GENERIC_WRITE,         FILE_SHARE_READ | FILE_SHARE_WRITE,         OPEN_EXISTING | CREATE_ALWAYS | TRUNCATE_EXISTING,         NULL);     if (fd == INVALID_HANDLE_VALUE)     {         ret = GetLastError();

    GetLastError() returns 87 "The parameter is incorrect."

    Opening it for read only as in below, it's fine.

        

    fd = CreateFile2(filename, GENERIC_READ,     FILE_SHARE_READ,     OPEN_EXISTING,     NULL);


    Tuesday, December 2, 2014 11:22 PM

Answers

  • This did the trick

    CreateFile2(filename,
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | 
        FILE_SHARE_WRITE | 
        FILE_SHARE_DELETE,
        CREATE_ALWAYS,
        NULL);

    • Marked as answer by lil'sprout Wednesday, December 3, 2014 8:22 PM
    Wednesday, December 3, 2014 8:21 PM

All replies

  • After CreateFile2 fails, this succeeds

    DeleteFile(filename);

    What's the CreateFile2() magic parameter combo, as I expect it should work...

    Wednesday, December 3, 2014 12:32 AM
  • Hi lil'sprout,

    If you would like to create a file, why not try a Windows Store App original File API StorageFile class? For instance you can create one Quickstart: Reading and writing files, you may also ref to a sample: ttps://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    I'm not sure why your code get "The parameter is incorrect", let's say if you run the code in a Desktop app, will the same error message shown to you?

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 3, 2014 5:07 AM
    Moderator
  • This did the trick

    CreateFile2(filename,
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | 
        FILE_SHARE_WRITE | 
        FILE_SHARE_DELETE,
        CREATE_ALWAYS,
        NULL);

    • Marked as answer by lil'sprout Wednesday, December 3, 2014 8:22 PM
    Wednesday, December 3, 2014 8:21 PM