Ra'ed Alaraj
Ra'ed Alaraj

Reputation: 173

Automatically refresh file list in a UWP app when files moved/added/renamed

I'm working on a simple music player app for UWP and I have a couple of questions. First of all here's my code

 private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            StorageFolder folder = KnownFolders.MusicLibrary;    
            await RetrieveFilesInFolders(folder);
        }



  private async Task RetrieveFilesInFolders(StorageFolder parent)
        {
            foreach (var file in await parent.GetFilesAsync())
            {
                if (file.FileType == ".mp3")
                {
                    var songProperties = await file.Properties.GetMusicPropertiesAsync();
                    var currentThumb = await file.GetThumbnailAsync(ThumbnailMode.MusicView, 200, ThumbnailOptions.UseCurrentScale);

                    var albumCover = new BitmapImage();
                    albumCover.SetSource(currentThumb);

                    var song = new Song();
                    song.Title = songProperties.Title;
                    song.Artist = songProperties.Artist;
                    song.Album = songProperties.Album;
                    song.AlbumCover = albumCover;
                    song.SongFile = file;
                    song.FileName = file.Name;

                    SongUC songUc = new SongUC(song);
                    sp1.Children.Add(songUc);
                }
            }

            foreach (var folder in await parent.GetFoldersAsync())
            {
                await RetrieveFilesInFolders(folder);
            }
        }

User control ctor

public SongUC(Song song)
        {
            this.InitializeComponent();
            txtTitle.Text = song.Title;
            txtAlbum.Text = song.Album;
            txtArtist.Text = song.Artist;
            txtName.Text = song.FileName;
            imgAlbumArt.Source = song.AlbumCover;
        }
  1. How can I refresh the returned files automatically whenever a new song is added or moved or renamed
  2. Is firing the RetrieveFilesInFolders method in the PageLoaded event handler the best way to get all songs. Or will it slow down the app if there's a huge collection of music in the music folder
  3. How can I use animations whenever the files are retrieved to make things look nicer

Upvotes: 3

Views: 381

Answers (2)

Marian Dolinský
Marian Dolinský

Reputation: 3492

As stated here, take a look at file queries and their ContentsChanged event.

Upvotes: 1

Palle Due
Palle Due

Reputation: 6292

Use a FileSystemWatcher:

https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

It allows you to catch events when files are being moved, deleted and renamed.

Upvotes: 0

Related Questions