Search code examples
c#media-playermediasaving-data

Reading out textfiles and saving the lines in String[]


So I am making a media player which you can use to listen to predefined radio stations and play your own music on.

I can already Load Songs and play them in a playlist.

I managed to save the songs in the list to a text file so you don't have to select every song again every time you want to hear it.

But i have trouble reading it out.

I have a string[] files and string[] paths.

which are used to save the file names and the paths of it.

It is saved as:

RandomSongInPlaylist.mp3||C:\where\ever\you\have\saved\it ASecondSongInPlaylist.mp3||C:\Another\map

Now i want to read it out and save the names before the || in the string[] files and the paths after the || in string[] paths.

Ill post some code of it:

//the code when you add songs to the playlist

private void btnAfspeellijst_Click(object sender, EventArgs e) //button for adding songs

    {
        Playlist.Items.Clear();
        Played.Items.Clear(); //this is for shuffle modus, ignore it.
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "Music Files (*.mp3, *.wav, *.wmv, *.wma *.mp4, *.wv, *.aac)|*.mp3; *.wav; *.wmv; *.wma; *.mp4; *.wv; *.aac|All Files (*.*)|*.*";
        openFileDialog1.Multiselect = true;
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            try
            {
                files = openFileDialog1.SafeFileNames;
                paths = openFileDialog1.FileNames;
                for (int i = 0; i < files.Length; i++)
                {
                    Playlist.Items.Add(files[i]);
                }
                Playlist.SelectedIndex = 0;
            }
            catch
            {
                MessageBox.Show("Voeg een nummer toe"); //add a nummer, for when you dont give numbers
            }
        }            
    }

//For when you save the playlist as a text file.

private void btnOpslaan_Click(object sender, EventArgs e) //button for saving it in textfile

    {
        var saveFile = new SaveFileDialog();
        saveFile.Filter = "Text (*.txt)|*.txt";
        if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            int i = 0;
            using (var sw = new System.IO.StreamWriter(saveFile.FileName, false))
                foreach (var item in files)
                {
                    sw.Write(item.ToString() + "||" + paths[i].ToString()+Environment.NewLine);
                    i++;
                }
            MessageBox.Show("Success");
        }
    }

Can someone help me reading the text file out again, when you press the open playlist button, to read the selected text file out and save everything before the || as a file in files and everything after the || as a path in paths?


Solution

  • Read all lines from file:
    MSDN ReadAllLines

    Then you can parse data using Split():

    List<string> files = new List<string>;
    
    foreach (string line in lines[]) 
    {
       files.Add(line.Split("||")[0]; 
       //... same code for paths
       // split will return file in element 0 and paths in element1
    }
    

    Side Note:
    Code like this would be easier to maintain:

    private void btnAfspeellijst_Click(object sender, EventArgs e)
    {
       mp3Files.OpenList();
    }
    

    ...and somewhere in your project

    public class MusicFilesHandler
    {
        public static OpenList()
        {
           //... code to open file
        }
    }   
    

    Separating Logic and View is generally accepted as good idea in programming.
    Read more about MVC or MVVM.