找不到存储文件夹文件错误

StorageFolder File Not Found error

我的应用程序在下载音乐文件列表时遇到了一些问题。我正在尝试设置以下文件夹结构。音乐库 > 艺术家 > 发行名称。开始下载时,第一首歌曲的文件夹结构已正确设置。第二次下载开始后,我在尝试创建第二个子文件夹(版本名称)时总是会收到“未找到文件”异常。这是我的代码。

    private async Task StartDownload(List<DownloadData> data)
    {
        foreach (DownloadData song in data)
        {
            // Set the source of the download
            Uri source = new Uri(song.downloadUrl);

            // Create folder stucture
            StorageFolder artistFolder;
            try
            {
                artistFolder = await KnownFolders.MusicLibrary.CreateFolderAsync(song.artistName, CreationCollisionOption.OpenIfExists);
            }
            catch
            {
                throw;
            }
            StorageFolder releaseFolder;
            try
            {
                releaseFolder = await artistFolder.CreateFolderAsync(song.releaseName, CreationCollisionOption.OpenIfExists);
            }
            catch
            {
                throw; // Exception Thrown here
            }

            // Create file
            StorageFile destinationFile;
            try
            {
                destinationFile = await releaseFolder.CreateFileAsync(song.fileName, CreationCollisionOption.GenerateUniqueName);
            }
            catch
            {
                throw;
            }

            BackgroundDownloader downloader = new BackgroundDownloader();
            DownloadOperation download = downloader.CreateDownload(source, destinationFile);

            List<DownloadOperation> requestOperations = new List<DownloadOperation>();
            requestOperations.Add(download);

            await HandleDownloadAsync(download, true);
        }
    }

我不知道为什么第一次能用,但第二首歌就失败了。

根据 documentation 对于 CreateFileAsync 它将抛出 FileNotFoundExcption if

The folder name contains invalid characters, or the format of the folder name is incorrect.

所以您可能需要用下划线等其他内容替换无效字符。

var fixedFolderName = string.Join(
    "_", 
    song.releaseName.Split(Path.GetInvaildFileNameChars()));