对C#中URI的字符串操作

本文关键字:字符串 操作 URI | 更新日期: 2023-09-27 18:24:09

我正在为iTunes XML文件编写一个解析器,并试图解析文件位置,以允许应用程序从文件夹中恢复folder.jpg。

我正在itunes.xml的位置创建一个URI,格式如下:

''localhost'C:'MP3 Collection'Álbuns'# - E'A'a-ha'[1985] Hunting High And Low'01. Take On Me.mp3

我需要提取01. Take On Me.mp3子串,并将其替换为folder.jpg

请记住,每个文件中的字符串都不同。

对C#中URI的字符串操作

您可以使用Path.GetDirectoryName方法和Path.Combine方法:

var s = @"''localhost'C:'MP3 Collection'Álbuns'# - E'A'a-ha'[1985] Hunting High And Low'01. Take On Me.mp3";
var result = Path.Combine(Path.GetDirectoryName(s), "folder.jpg");
// result == @"''localhost'C:'MP3 Collection'Álbuns'# - E'A'a-ha'[1985] Hunting High And Low'folder.jpg"