2016-04-14 54 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])); 
    } 
} 

你外环实际运行不止一次,而是因为你重新初始化播放列表,在外循环结束时唯一保持的状态是最后一次迭代,这给出了仅运行一次的外观。