嗨,大家好,我正在阅读包含播放列表的文件。 (行文件位置)Loop只会触发一次
我现在有格式(名称; c:\ song.mp3; c:\ song.mp3; c:\ song.mp3; c:\ song.mp3等等)
文件中有很多这些行。
我曾经尝试都foreach
循环和for循环,试图解决这个问题(如下图所示)
string[] lines = File.ReadAllLines("playlists.txt");
MessageBox.Show(lines.Count().ToString());
for (int y = 0; y <= lines.Count(); y++)
{
string[] res = lines[y].Split(';');
for (int x = 0; x <= res.Count(); x ++)
{
if (x == 0) { currentPlaylist = new Playlist(res[x]); }
else { currentPlaylist.Add(new MP3(res[x])); }
}
}
但由于某些原因,它只会循环一次(我已经取代了foreach
外环其有同样的结果。
即使lines.Count()在MessageBox显示显示出更大的一个号码,然后1
我敢肯定,这一次解决它必须是基本的错误,但是我失去了
感谢
编辑*这是文件不知道这将如何帮助...
Library;C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3
playlist1;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Can t Stop.mp3;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside .mp3
playlist2;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside .mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - Best Of You.mp3
playlist3;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside.mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - The Pretender.mp3
playlist4;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - Everlong.mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - My Hero.mp3;C:\Users\Blah\Desktop\Iphone Music\I Am Giant - City Limits.mp3
我把它作为代码,所以它会更容易阅读
唯一我遇到的问题是,内循环只发射一次,我不确定为什么。
东西在下面....
for (int x = 0; x <= res.Count(); x ++)
{
if (x == 0) { currentPlaylist = new Playlist(res[x]); }
else { currentPlaylist.Add(new MP3(res[x])); }
}
导致外环只火一次,无论在代码行的量,如果删除了内环外环循环预期次数
您的预期成果是什么?每当'x'变为0时(每次'y'增加一次),你就会覆盖'currentPlayList',所以'currentPlaylist'只会包含最后一行的MP3。 –
你在哪里声明变量currentPlaylist?另外为什么你不能使用lines.Length而不是lines.Count()? res.Count()也一样。 – Sarathy
我的文件有5行左右,因此我预计这会创建5个左右的播放列表。 –