lunes, 27 de julio de 2015

Contenido de un listbox a un archivo plano

Separar un String en torno a otro (Split con String)
Para agregar los items de ubn list box a un archivo es muy sencillo se usa el siguiente codigo:

        private void BtnToCSV_Click(object sender, EventArgs e)
        {
            String Correccion = "";
            foreach (String Line in listBox2.Items)
            {
                Correccion = Correccion + Line + "\n";
            }

            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Filter = "csv files (*.csv)|*.csv";
            dlg.Title = "Export in CSV format";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (dlg.FileName != "")
                {
                    //Pass the filepath and filename to the StreamWriter Constructor
                    StreamWriter sw = new StreamWriter(dlg.FileName);

                    //Write a line of text
                    sw.Write(Correccion);

                    //Close the file
                    sw.Close();
                }
            }
}

No hay comentarios:

Publicar un comentario