[SOLUCIONADO] Guardar listbox en un txt

(1/1)

.:Dione:.:
OS cuento el problema, que será facil de resolver, pero no lo veo.
Tengo mi programa que hace una lista de las carpetas de un directorio y lo guarda en un listbox, hasta aquí todo bien, el problema llega cuando intento guardar el contenido del listbox en un txt.

Os pongo el código y el contenido del txt.
Código
 
Option Explicit On
 
Imports System.IO
Public Class Form1
 
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
   End Sub
 
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       For Each archivos As String In Directory.GetDirectories("c:\", "*.*", SearchOption.TopDirectoryOnly)
           ' extraer el nombre de la carpeta de la ruta completa  
           archivos = archivos.Substring(archivos.LastIndexOf("\") + 1).ToString
           ' Agregar el valor  
           ListBox1.Items.Add("C:\" & archivos.ToString)
 
       Next
 
 
 
   End Sub
 
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim oSW As New StreamWriter("C:\archivo_prueba.txt")
 
       Dim Linea = ListBox1.Items.ToString
       oSW.WriteLine(Linea)
       oSW.Flush()
 
   End Sub
 
   Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
 
   End Sub
End Class
 

Contenido del txt.
Código
System.Windows.Forms.ListBox+ObjectCollection

Espero que me podais ayudar.

Erik#:
Esto es de C#, pero puede servirte: http://msdn.microsoft.com/es-es/library/8bh11f1k.aspx

Atrum:
El problema esta aqui:

Código
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim oSW As New StreamWriter("C:\archivo_prueba.txt")
 
       Dim Linea = ListBox1.Items.ToString  'El objeto ListBox1.Items es del tipo ListBoxObjectCollection y al convertirlo a cadena no obtienes el texto
                                                        'ya que ListBox1.Items es un arreglo de items
       oSW.WriteLine(Linea)
       oSW.Flush()
 
   End Sub
 

Cambialo por:

Código
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
       Dim oSW As New StreamWriter("C:\Users\dguatemala\archivo_prueba.txt")
       Dim i As Integer
       For i = 0 To ListBox1.Items.Count - 1
           Dim Linea = ListBox1.Items(i).ToString() 'Asi obtienes cada elemento del arreglo y conviertes su valor a cadena
           oSW.WriteLine(Linea)
           oSW.Flush()
       Next
 
 
   End Sub
 

Eso, en teoria, te deberia funcionar como quieres, espero te sirva

.:Dione:.:
Gracias, ese código ha funcionado perfectamente. Dentro de poco os volveré a molestar con mis preguntas jeje. Has pronto y muchas gracias.

Navegación

[0] Índice de Mensajes