Until recently, everything was fine and here, the code that I did not change, began to do something irresponsible, namely, to throw out the absolute path from Uri on the local computer:
Codes needed to understand (or maybe not):
The method in which the exception is generated:
public static string GetSongsName(string mp3filePath) { //Строка которая выкидывает ошибку (что логично, судя по absolutepath из передаваемого Uri): Mp3File mp3tags = new Mp3File(mp3filePath); System.IO.FileInfo file = new System.IO.FileInfo(mp3filePath); string[] tags = new string[2]; if (mp3tags.HasTags) { tags[0] = mp3tags.GetTag(Id3TagFamily.FileStartTag).Title; tags[1] = mp3tags.GetTag(Id3TagFamily.FileStartTag).Artists; } if (tags[0] != null && tags[1] != null && !tags[0].Equals("") && !tags[1].Equals("")) { return tags[0] + " - " + tags[1]; } else { if (!HelpsMethod.GetTitle(mp3filePath).Equals("")) { return tags[0]; } else { return file.Name.Remove(file.Name.Length - 4, 4); } } } The class that contains the Uri type field
public class Song { private static int Counter = 0; #region Fields private string _artist; private string _title; private string _absolutePath; private int _duration; private int _number; private bool _isLocal; //Мой вопрос касается этого поля: private Uri _songUri; #endregion #region Properties public string Artist { get { return _artist; } set { _artist = value; } } public string Title { get { return _title; } set { _title = value; } } public string AbsolutePath { get { return _absolutePath; } set { _absolutePath = value; } } public int Duration { get { return _duration; } set { _duration = value; } } public int Number { get { return _number; } set { _number = value; } } public bool IsLocal { get { return _isLocal; } set { _isLocal = value; } } //Мой вопрос касается этого поля: public Uri SongUri { get { return _songUri; } set { _songUri = value; } } #endregion public Song(string Title, bool IsLocal, Uri SongUri, string Artist = null, int Duration = 0) { this.Title = Title; this.Duration = Duration; this.IsLocal = IsLocal; this.Artist = Artist; //Мой вопрос касается этого поля: this.SongUri = SongUri; Number = ++Counter; } } Method that calls a method that throws an exception:
private void addToPlaylist() { OpenFileDialog openMusic = new OpenFileDialog(); try { openMusic.Filter = "All files (*.*)|*.*|MP3 Music (*.mp3)|*.mp3"; openMusic.FilterIndex = 2; openMusic.Multiselect = true; openMusic.Title = "Select a music file"; if (openMusic.ShowDialog() == true) { foreach (string item in openMusic.FileNames) { FileInfo f = new FileInfo(item); Mp3File mp3tags = new Mp3File(item); string[] tags = new string[2]; if (mp3tags.HasTags) { tags[0] = mp3tags.GetTag(Id3TagFamily.FileStartTag).Title; tags[1] = mp3tags.GetTag(Id3TagFamily.FileStartTag).Artists; } if (tags[0] != null && tags[1] != null && !tags[0].Equals("") && !tags[1].Equals("")) { ModelMainWindow.SongsPlaylist.Add( new Song(tags[0], true, new Uri(item) , tags[1])); } else { if (!HelpsMethod.GetTitle(item).Equals("")) { ModelMainWindow.SongsPlaylist.Add( new Song(HelpsMethod.GetTitle(f.Name). Remove(HelpsMethod.GetTitle(f.Name).Length - 4, 4), true, new Uri(item), HelpsMethod.GetArtist(f.Name))); } else { ModelMainWindow.SongsPlaylist.Add( new Song("", true, new Uri(item), f.Name.Remove(f.Name.Length - 4, 4))); } } } } ModelMainWindow._MediaPlayer.Open(ModelMainWindow.SongsPlaylist[ModelMainWindow.Index].SongUri); //Вот здесь смотрим содержимое абсолютного пути из нашего Uri System.Windows.MessageBox.Show(ModelMainWindow.SongsPlaylist[ModelMainWindow.Index].SongUri.AbsolutePath); //Вот тут кроется ошибка (при вызове метода "GetSongsName" после передачи ему абсолютного пути //из Uri ModelMainWindow.SongName = Helpers.HelpsMethod.GetSongsName (ModelMainWindow.SongsPlaylist[ModelMainWindow.Index].SongUri.AbsolutePath); } catch (Exception e) { Console.Error.WriteLine(e.Message); } } I repeat, none of this code was changed, just suddenly Uri when using AbsolutePath instead of the line in the form "D: \ file \ file.file" began to give what was shown in the screenshot. I would be very grateful if someone tells you what could be the problem.
UPD. After a night, I didn’t understand what could have happened to Uri.AbsolutePath. Made different, but less nice = \
Who knows, give an answer, what is wrong with Uri when working with local files is terribly interesting.
