0

Wow, is this way more complicated than it needs to be. Can someone explain to me why the following code works:

       string stringToWrite = "SomeStuff";
        Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
        Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
        var files = await installedLocation.GetFilesAsync();
        foreach (Windows.Storage.StorageFile sf in files)
        {
            if (sf.Name.Equals("log.txt"))
            {
                await FileIO.AppendTextAsync(sf, stringToWrite);

            }
        }

And yet the following fails with AccessDenied:

      Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
      Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
      var log = await installedLocation.GetFileAsync("log.txt");
      await FileIO.AppendTextAsync(log, stringToWrite);

The only difference is looping through the files returned by the GetFilesAsync method vs getting the file by name. By the way, getting the file by name works because if I misspell log.txt in GetFileAsync, I get an exception.

Very confusing....

svick
  • 236,525
  • 50
  • 385
  • 514
mrasmussen
  • 55
  • 1
  • 7
  • this line is the difference in both of your examples `var files = await installedLocation.GetFilesAsync();` the method according to MSDN `Gets the top-level files in the current folder` – MethodMan Jan 21 '13 at 19:06
  • I know, one get a single storageFile and the other returns a collection which I then loop through the get the single file. File access should be the same as far as read/write is concerned, no? – mrasmussen Jan 21 '13 at 19:08
  • Are you talking about the Windows Runtime or are you talking about the OS called Windows RT? – Charles Jan 21 '13 at 21:17

1 Answers1

1

You should not be using your installed location to write any files. It is supposed to be read-only as per MSDN: File Access/Permissions in Windows Store Apps:

The app's install directory is a read-only location. You can’t gain access to the install directory through the file picker.

You should be using either the Local, Roaming, or Temporary storage locations.

See this link: MSDN: Quickstart Local Application Data

Erik
  • 12,730
  • 5
  • 36
  • 42
  • that did seem to work, thanks SiLo. Not sure why I could write to the file if I looped through the collection, but not if I got the handle to the file by name. I should have seen that install path wasn't a recommended place to write to. – mrasmussen Jan 21 '13 at 19:44
  • How did you create `log.txt` the first time? I have tried to replicate this, and the only way I could even get the file in there was to create the file via Windows Explorer first, then it would work. I got access denied exceptions trying to Create/Append to the file in code. – Erik Jan 21 '13 at 19:48
  • I added the file via Visual Studio 2012 to the root of the project as a text file. – mrasmussen Jan 21 '13 at 20:31
  • Ah! That would make sense. It probably copied the permissions it had with it. So that file may have been read/write, but not what you should expect from that location. – Erik Jan 21 '13 at 20:35