locked
CreateFile2 gets access denied error when trying to open file in local app folder

    Question

  • The code i am using is below the text. Basically I've included a file in the project called "savefile.bin" and i tagged it as "content" so it gets copied to the local app folder. I check the local app folder, which is located for me when debugging:

    "C:\Users\[username]\AppxLayouts\[projectname]VS.Debug.Win32.[username]".

    The folder contains neatly the "savefile.bin" file. But when running the code i get an "ACCESS_DENIED 0x05" error. When i take a look at the file properties and specifically the security tab, i can see 4 user's.

    SYSTEM

    Administrators

    mine

    and then there is a strange one 'Account unknown (S-1-15-2-64412......)'. (I am guessing that is the sandbox account)

    Thats also the one with only read and execute rights. When i give that account modify and write access the code actually runs perfectly fine.

    Am i doing something wrong? Or is there maybe a way to give the sandbox account access rights? (If you were wondering, opening and reading the file works perfectly fine)

     

    StorageFolder^ baseFolder = Package::Current->InstalledLocation;
    Platform::String^ location = baseFolder->Path;
    Platform::String^ createFileString = Platform::String::Concat(location, "\\savefile.bin");
    
    Wrappers::FileHandle file( 
          CreateFile2(
                 createFileString->Data(), 
                 GENERIC_READ | GENERIC_WRITE, 
                 0, 
                 TRUNCATE_EXISTING,
                 nullptr ) );
    
    DWORD dw = GetLastError(); //returns 0x05
    //http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=VS.85).aspx
    
    if (file.Get() != INVALID_HANDLE_VALUE)
    {
    	//Write stuff to the file
    }


     

    • Edited by Gorbear Wednesday, November 30, 2011 3:55 PM Fixed looks
    Wednesday, November 30, 2011 3:49 PM

Answers

  • As you note, the application cannot write to its install directory.  Instead it should store writable content in its app data store.  See the Application data topic for more information.

    --Rob

    • Marked as answer by Gorbear Thursday, December 1, 2011 7:36 PM
    Wednesday, November 30, 2011 11:27 PM
    Owner

All replies

  • As you note, the application cannot write to its install directory.  Instead it should store writable content in its app data store.  See the Application data topic for more information.

    --Rob

    • Marked as answer by Gorbear Thursday, December 1, 2011 7:36 PM
    Wednesday, November 30, 2011 11:27 PM
    Owner
  • I fixed it by accessing the correct path taken from what Rob. 

     

    C++ version of the code looks a bit like this:

     

     StorageFolder^ baseFolder = Windows::Storage::ApplicationData::Current->LocalFolder
    Thursday, December 1, 2011 7:39 PM