Question

I am trying to develop an application for Windows RT devices. The issue that I face currently is based on the next use-case scenario:

  • The user defines a structure named "Song" that has various fields (title, artist, etc.), including an audio file selected using the FileOpenPicker class.

I would like to save this structure in a JSON file that contains the provided information (title, artist, FullPathToAudio, etc) and load the structure later. After opening the existing songs, I would like to access the audio file and provide a play button.

The problem is that when I try to access the file via the StorageFile.GetFileFromPathAsync(currentSong.FullPathToAudio) the application closes. I have checked the Removable Storage box from the Capabilities tab of the Package.appxmanifest file and added a FileTypeAssociation for audio files (MP3).

The Package.AppxManifest file was changed as following:

<Applications>
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="JamAid.App">
      <m2:VisualElements DisplayName="JamAid" Square150x150Logo="Assets\Logo.png" Square30x30Logo="Assets\SmallLogo.png" Description="JamAid" ForegroundText="light" BackgroundColor="#464646">
        <m2:SplashScreen Image="Assets\SplashScreen.png" />
      </m2:VisualElements>
      <Extensions>
        <Extension Category="windows.fileTypeAssociation">
          <FileTypeAssociation Name="mpfiles">
            <DisplayName>Mp3 Files</DisplayName>
            <EditFlags OpenIsSafe="true" />
            <SupportedFileTypes>
              <FileType ContentType="audio/mp3">.mp3</FileType>
            </SupportedFileTypes>
          </FileTypeAssociation>
        </Extension>
      </Extensions>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="removableStorage" />
  </Capabilities>

Because the user might create playlists with lots of songs, copying the files in the application directory or other accessible location is not an option...

Edit: I have tryed to debug the application on the local machine. Now, instead of just closing, it gives the following error:

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

WinRT information: Cannot access the specified file or folder (讠㣯ü). The item is not in a location that the application has access to (including application data folders, folders that are accessible via capabilities, and persisted items in the StorageApplicationPermissions lists). Verify that the file is not marked with system or hidden file attributes.

Additional information: Access is denied.

If there is a handler for this exception, the program may be safely continued.

Was it helpful?

Solution

I found out that I could use this solution: http://www.jonathanantoine.com/2012/08/06/keep-access-to-filesfolders-the-user-granted-you/

Basically one would have to store the location in the FutureAccessList:

var picker = new FolderPicker();
picker.FileTypeFilter.Add("*.mp3");
var folder = await picker.PickSingleFolderAsync();

StorageApplicationPermissions.FutureAccessList.AddOrReplace(Token, folder);

And get it back using:

var folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(Token);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top