Eng Soon Cheah
Eng Soon Cheah

Reputation: 257

UWP- System Exception : The Specified path is invalid

Currently I had developed Windows 10 Mobile Apps that can play sounds/audio functions. When I write this statement to play the audio/sounds , it will display the error when the Image Tapped event is Tapped. The Source code as below:

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

The Error System Exception : The Specified path is invalid

is that any solutions for this?

Thank You.

Upvotes: 0

Views: 1183

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32785

The problem is that you have used wrong folder path.

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

Please use the following code to replace your folder path.

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

For more you could refer to File access permissions.

Upvotes: 1

Related Questions