【问题标题】:StorageFolder File Not Found error未找到 StorageFolder 文件错误
【发布时间】:2015-03-13 17:08:56
【问题描述】:

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

    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);
        }
    }

我不知道为什么它第一次奏效,但第二首歌却失败了。

【问题讨论】:

  • 你为什么有这么多catch{throw;}。要么抓住并做某事,要么根本不抓住。
  • @juharr 我不知道如何处理异常。这就是我问这个问题的原因。
  • 根据docs CreateFileAsync throws FileNotFoundException if "文件夹名称包含无效字符,或者文件夹名称格式不正确。"那么song.releaseName的值是多少呢?
  • 好收获。发布名称中有几个“/”。那么我唯一的选择是删除那些吗?

标签: c# windows-8 windows-phone-8.1


【解决方案1】:

根据documentationCreateFileAsync 它将抛出FileNotFoundExcption 如果

文件夹名称包含无效字符,或文件夹名称格式不正确。

因此,您可能需要用下划线之类的其他内容替换无效字符。

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

【讨论】:

  • 由于某种原因,使用GetInvalidPathChars() 不会替换“/”,但将其切换为GetInvaildFileNameChars() 可以解决问题。
  • 是的,“/”是一个有效的路径字符,特别是路径分隔符。我认为这更像是GetInvalidFolderChars(),但文件名 1 是您真正想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 2016-06-05
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多