Search code examples
c#filewindows-phone-8windows-phone-8.1

how to delete library file permanently on Windows Phone 8?


I try to use the new api of Windows Phone 8.1 . I heard i can remove Library file from application. But when i try to use the function "StorageFile.DeleteAsync" , i just see the file removed from the localStorage and no from the music Library ( the true storage).

Can i really delete a file from the Library with Windows phone 8.1 O.S ?

I just want know how implemant it. I know it's possible , they have a lot of application how allow manage file (read,write) like "Aerize Explorer" for example

This is the code i use:

    #region Main
    public Main()
    {
        Task.Run(async () => Global.Explorer.DeleteMusicFile(await KnownFolders.MusicLibrary.GetFileAsync("title.mp3")));
    }
    #endregion

    public class FileExplorer
    {
        // DeleteFile
        public async Task DeleteMusicFile(StorageFile fileToDelete)
        {
            await fileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete);
        }
    }

Solution

  • To answer your question:

    Can i really delete a file from the Library with Windows phone 8.1 O.S ?

    Yes, you can - here is an example of an App (WP8.1 Runtime) which does it - whach out when testing - it deletes the file pernamently, without prompting ;). It mostly consists of lines:

    var listBefore = (await KnownFolders.MusicLibrary.GetFilesAsync()).ToList();
    await listBefore.FirstOrDefault().DeleteAsync(StorageDeleteOption.PermanentDelete);
    var listAfter = (await KnownFolders.MusicLibrary.GetFilesAsync()).ToList();
    

    I get the list of StorageFiles, then I delete the first of them. As a result, the second time I get the list of files - it's lack one file (the deleted one). So it works.

    Remember to add Capabilities: MusicLibrary and/or Removable Storage. And watch out if the file you want to delete is not used elsewhere.

    As I've spotted that I'm not able to refresh the files which are on the phone in Explorer Window (browsing it in Windows via USB). To see the result (file missing), I had to unplug and plug the phone again.