Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: .:Dione:. en 10 Septiembre 2009, 14:07 pm



Título: [SOLUCIONADO] Guardar listbox en un txt
Publicado por: .:Dione:. en 10 Septiembre 2009, 14:07 pm
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
  1.  
  2. Option Explicit On
  3.  
  4. Imports System.IO
  5. Public Class Form1
  6.  
  7.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.  
  9.    End Sub
  10.  
  11.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  12.        For Each archivos As String In Directory.GetDirectories("c:\", "*.*", SearchOption.TopDirectoryOnly)
  13.            ' extraer el nombre de la carpeta de la ruta completa  
  14.            archivos = archivos.Substring(archivos.LastIndexOf("\") + 1).ToString
  15.            ' Agregar el valor  
  16.            ListBox1.Items.Add("C:\" & archivos.ToString)
  17.  
  18.        Next
  19.  
  20.  
  21.  
  22.    End Sub
  23.  
  24.    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  25.        Dim oSW As New StreamWriter("C:\archivo_prueba.txt")
  26.  
  27.        Dim Linea = ListBox1.Items.ToString
  28.        oSW.WriteLine(Linea)
  29.        oSW.Flush()
  30.  
  31.    End Sub
  32.  
  33.    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  34.  
  35.    End Sub
  36. End Class
  37.  

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

Espero que me podais ayudar.


Título: Re: Guardar listbox en un txt
Publicado por: Erik# en 10 Septiembre 2009, 14:39 pm
Esto es de C#, pero puede servirte: http://msdn.microsoft.com/es-es/library/8bh11f1k.aspx


Título: Re: Guardar listbox en un txt
Publicado por: Atrum en 10 Septiembre 2009, 15:58 pm
El problema esta aqui:

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

Cambialo por:

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

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


Título: Re: Guardar listbox en un txt
Publicado por: .:Dione:. en 10 Septiembre 2009, 23:53 pm
Gracias, ese código ha funcionado perfectamente. Dentro de poco os volveré a molestar con mis preguntas jeje. Has pronto y muchas gracias.