如何用c#(文本文件)创建单独的行

时间:2012-10-10 10:19:28

标签: c# split streamreader readline

我正在使这个应用程序很有趣,但我有一个问题

我希望以单独的行读取此字符串/文件。

这个文件(不是整个文件):

1ChampSelectPack.Ahri.mp31ChampSelectPack.Akali.mp31ChampSelectPack.Alistar.mp31ChampSelectPack.Amumu.mp31ChampSelectPack.Anivia.mp31ChampSelectPack.Annie.mp31ChampSelectPack.Ashe.mp31ChampSelectPack.Blitzcrank.mp31ChampSelectPack.Brand.mp31ChampSelectPack.Caitlyn.mp3

这是我到目前为止所得到的:

            List<SoundPath> paths = new List<SoundPath>();
            StreamReader reader = File.OpenText("C:/Users/Esat/Documents/Visual Studio 2010/Projects/WikiLoL/WikiLoL/lolSoundBoard/1ChampSelectPack/files.txt");

            while (!reader.EndOfStream)
            {

                SoundPath path = new SoundPath();
                path.Path = reader.ReadLine();
                paths.Add(path);
            }

            reader.Close();

            return paths;

2 个答案:

答案 0 :(得分:2)

不确定这是否是你想要的:

"YourString".Split(new string[] {"mp3"}, StringSplitOptions.None)

之后你必须在每一行上附加“mp3”。

答案 1 :(得分:1)

您可以使用拆分.mp3并在结果数组的每个元素中添加.mp3来实现。

string text = File.ReadAllText("C:/Users/Esat/Documents/Visual Studio 2010/Projects/WikiLoL/WikiLoL/lolSoundBoard/1ChampSelectPack/files.txt");
string[] lines = text.Split(new string[] { ".mp3" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < lines.Length; i++)
    lines[i] = lines[i] + ".mp3";