嗨,大家好,我正在閱讀包含播放列表的文件。 (行文件位置)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個左右的播放列表。 –