2016-04-14 136 views
2

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

導致外環只火一次,無論在代碼行的量,如果刪除了內環外環循環預期次數

+2

您的預期成果是什麼?每當'x'變爲0時(每次'y'增加一次),你就會覆蓋'currentPlayList',所以'currentPlaylist'只會包含最後一行的MP3。 –

+0

你在哪裏聲明變量currentPlaylist?另外爲什麼你不能使用lines.Length而不是lines.Count()? res.Count()也一樣。 – Sarathy

+0

我的文件有5行左右,因此我預計這會創建5個左右的播放列表。 –

回答

0

根據您的問題和評論你寫,我認爲你正在尋找的東西,如:

// This method read the file and populate a List of Playlist. 
// Each PlayList contain a list of MP3 songs 
public List<Playlist> GetPlayLists() 
{ 
    // read all lines from the text file 
    string[] lines = File.ReadAllLines(@"c:\Temp\playlists.txt"); 

    // declare a playlists variable to hold all the playlists and their songs 
    var playlists = new List<Playlist>(); 

    // Loop through all the playlists (each line in the text file represents a playlist) 
    for (int plIdx = 0; plIdx < lines.Length; plIdx++) 
    { 
     // split in order to fins all the MP3 songs 
     string[] res = lines[plIdx].Split(';'); 

     // create a new playlist (its name is passed into the constructor) 
     var playlist = new Playlist(res[0]); 

     // loop the songs (starting from index 1 since index=0 is the playlist name) 
     for (int songIdx = 1; songIdx < res.Length; songIdx++) 
     { 
      // Add to the playlist each song 
      playlist.Add(new MP3(res[songIdx])); 
     } 
     playlists.Add(playlist); 
    } 

    return playlists; 
} 

// Play list class containing all the MP3 songs (each line in text file) 
class Playlist 
{ 
    public List<MP3> SongList { get; private set; } 
    public string Name { get; private set; } 

    public Playlist(string name) 
    { 
     Name = name; 
     SongList = new List<MP3>(); 
    } 

    public void Add(MP3 mp3) 
    { 
     SongList.Add(mp3); 
    } 
} 

// MP3 Song class 
class MP3 
{ 
    public string Location { get; private set; } 
    public MP3(string location) 
    { 
     Location = location; 
    } 
} 

這是播放列表填充後播放列表變量的外觀。txt文件:

enter image description here

做的,以使其發揮作用以下步驟。一旦工作,你將有一個參考,所以你可以簡單地將它合併到你現有的項目。

創建一個新的控制檯應用程序 你的類節目:

​​

創建的FileReader類,並把GetPlayLists方法爲

class FileReader 
{ 
    // This method read the file and populate a List of Playlist. 
    // Each PlayList contain a list of MP3 songs 
    public List<Playlist> GetPlayLists() 
    { 
     // ..... 
    }   
} 

把其他類播放列表和MP3 。

現在你應該可以沒有任何問題地運行它。

+0

可悲的是,這沒有讀取第一行......我現在非常困惑,錯誤是什麼,爲什麼它只是循環一次 –

+0

我在代碼中修復了一個問題。我用你發佈的文件運行它,它工作正常。請再次嘗試(注意,該文件需要在c:\ temp \ playlists.txt – ehh

+0

它不起作用,即使第一行沒有任何反應,該文件在項目中,因此使用File.ReadAllLines(「Playlists.txt 「);應該工作,我的代碼工作的第一行,我想知道如何編輯它的工作,剩下的就是我所要求的全部問題 –

0

就我所見,你的代碼應該拋出異常。在循環條件中使用< =,但是當x == res.Count()不能訪問res[x]時,因爲索引必須介於0和Count-1之間。

嘗試至少用<代替<=

並且不要在陣列上使用Count()。他們有.Length財產。

+0

我用<替換所有<=並且count()與.Length和結果保持不變,圖書館閱讀正常,但其他人不會閱讀。 –

0

假設您發佈的文件是您正在閱讀的文件,則每行包含mp3的單個文件位置。用';'分隔每行後您可以安全地假定新陣列的第二項是文件路徑。

string[] lines = File.ReadAllLines("playlists.txt"); // 100 lines 

for (int y = 0; y < lines.Length; y++) loops through array index 0 to 99 
{ 
    string[] res = lines[y].Split(';'); // lines[y] should be something like c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3 
    // res[0] = Library 
    // res[1] = C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3 
    currentPlaylist = new Playlist(res[0].Trim()); 
    currentPlaylist.Add(new MP3(res[1].Trim())); 
} 

或者,如果每行有多個文件路徑..

string[] lines = File.ReadAllLines("playlists.txt"); // 100 lines 

for (int y = 0; y < lines.Length; y++) loops through array index 0 to 99 
{ 
    string[] res = lines[y].Split(';'); // lines[y] should be something like c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3 
    // res[0] = Library 
    // res[1] = C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3 
    currentPlaylist = new Playlist(res[0].Trim()); // res[0] is the playlist name right? 
    for(int x = 1; x < res.Length; x++) 
    { 
     currentPlaylist.Add(new MP3(res[x].Trim())); 
    }   
} 
+0

我的問題是,我可以用上面的代碼完美地讀取圖書館行,但循環只會觸發一次(對於文件的第一行),然後我失去了其餘的信息....會有很多每行多一個mp3 –

+0

我知道錯誤在中心循環內,因爲我只是從中刪除了代碼,並且它像我想要的那樣循環了5次。 –

+0

我用你的代碼替換了我的代碼來測試,但是我得到了相同的結果,我嘗試添加一個messagebox.show(「true」);在你的for循環之後,它甚至沒有激發一次.... –

0

試試這個,你不應該在內環初始化currentPlayList,因爲它會得到到y

重新初始化每次循環時間
Playlist currentPlaylist; 
string[] lines = File.ReadAllLines("playlists.txt"); 

for (int y = 0; y < lines.Length; y++) 
{ 
    if (y == 0) { 
     currentPlaylist = new Playlist(); 
    } 

    string[] res = lines[y].Split(';'); 
    for (int x = 0; x < res.Length; x ++) 
    { 
     currentPlaylist.Add(new MP3(res[x])); 
    } 
} 

你外環實際運行不止一次,而是因爲你重新初始化播放列表,在外循環結束時唯一保持的狀態是最後一次迭代,這給出了僅運行一次的外觀。