tags:

views:

104

answers:

4

HI all, i wish save in a file.txt the chronology of the song played by my music Player . I tried this code snippet but doesn't work :

    StreamWriter sw = new StreamWriter(@"c:\Media PlayList\List.txt");

   private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

    {
        wmp.URL = Convert.ToString(listBox1.SelectedItem);

foreach (object o in listBox1.SelectedItems) 

         {
               sw.WriteLine(DateTime.Now + " - " + o.ToString());          
         }
    }

How i store songs :

private List<string> GetFolder(string Folder)
    {

        string filena;
        DirectoryInfo dir = new DirectoryInfo(Folder);
        FileInfo[] files = dir.GetFiles("*.mp3", SearchOption.AllDirectories);

        List<string> str = new List<string>();

        foreach (FileInfo file in files)
        {
            str.Add(file.FullName);    }
    }


private void Form2_Load(object sender, EventArgs e)
    {

       List<string> uno = GetFolder(@"D:\\Music\\");
        listBox1.DataSource = uno;
        listBox1.DisplayMember = "uno"; }

I need each time the Music Player change song the file "List.txt" will be update by listbox.SelectedItem but i cannot update the "List.txt" with my code. Where i wrong? Thanks for your attention.

Nice regards

EDIT: I updated my code snippet hoping it will be clear .

A: 

Are there any exceptions? Or the file just wind up empty? What did you bind to the list box?

Ferry
HI Ferry,there are not exceptions and the "List.txt" is empty.In the ListBox i bind mp3 from a directory : List<string> uno = GetFolder(@"D:\\Music\\"); listBox1.DataSource = uno; listBox1.DisplayMember = "uno";so when i click on the lisBox i can change songs and i wish update the cronology in the "List.txt".I will update my question.Thanks for your reply
JayJay
+1  A: 

If it's failing to update the file, you may wish to set the append parameter in the constructor, eg:

StreamWriter sw = new StreamWriter(@"c:\Media PlayList\List.txt", true);

If it's failing to trigger the SelectedIndexChanged event, please update the question to reflect that...

Overflew
+1  A: 

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

{
    wmp.URL = Convert.ToString(listBox1.SelectedItem);

    foreach (object o in listBox1.SelectedItems) 
           sw.WriteLine(DateTime.Now + " - " + o.ToString());          
   sw.close();
}
+1  A: 
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = GetFolder("D:\\Music\\");
        }

        private static List<string> GetFolder(string folder)
        {
            List<string> FileList = new List<string>();
            foreach (FileInfo file in new DirectoryInfo(folder).GetFiles("*.mp3", SearchOption.AllDirectories))
                FileList.Add(file.FullName);
            return FileList;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter("c:\\Media PlayList\\List.txt", true);
            wmp.URL = Convert.ToString(listBox1.SelectedItem);
            foreach (object o in listBox1.SelectedItems)
                sw.WriteLine(DateTime.Now + " - " + o);
            sw.Close();
        }
    }
}

The above code will do the work for you. Notes:

  • If you want to append to the file, you should pass true as the second argument when you create the StreamWriter.
  • After writing your data to the file, you should explicitly close the StreamWriter, or your file will be empty.
  • After closing the file you can no longer write to it. If you want you can reopen it.
M. Jahedbozorgan
HI Jahedbozorgan,that's work fine infact i wrong becasue i close the file and it was always empty ,with your cone snippet i work out the trouble.Thanks so much.
JayJay