UWP- 系统异常:指定的路径无效

UWP- System Exception : The Specified path is invalid

目前我开发了Windows 10个可以发挥sounds/audio功能的手机App。 当我写这个语句来播放 audio/sounds 时,它会在点击 Image Tapped 事件时显示错误。 源代码如下:

MediaElement mysong = new MediaElement();

        try
        {
            var folder = await StorageFolder.GetFolderFromPathAsync(@"ms-appx://Assets/Media/");
            if (folder != null)
            {
                var file = await folder.GetFileAsync("police_alarm.mp3");
                if (file != null)
                {
                    var stream = await file.OpenReadAsync();
                    mysong.SetSource(stream, file.ContentType);
                    mysong.Volume = 100;
                    mysong.Play();
                }
            }
            else
            {
                MessageDialog dialog = new MessageDialog("Siren can't play !!! Please keep yourself safe !!!", "Error");
                await dialog.ShowAsync();
            }
        }
        catch(Exception ex)
        {
            MessageDialog dialog = new MessageDialog(ex.ToString(), "Error");
            await dialog.ShowAsync();
        }

错误

有解决办法吗?

谢谢。

问题是您使用了错误的文件夹路径。

var folder = await StorageFolder.GetFolderFromPathAsync(@"ms-appx://Assets/Media/");

请使用以下代码替换您的文件夹路径。

string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets\Media";
var folder = await StorageFolder.GetFolderFromPathAsync(path);

更多可以参考File access permissions.