Ver Mensaje Individual
  #6 (permalink)  
Antiguo 06/07/2008, 08:27
Avellaneda
Colaborador
 
Fecha de Ingreso: enero-2008
Ubicación: Unas veces aquí, otras veces allí
Mensajes: 1.482
Antigüedad: 17 años, 2 meses
Puntos: 37
Respuesta: Visual Basic 2005: Grabar y leer .ini

No, es que está utilizando un fichero con una única sección sin llaves (Keys) por lo tanto no le vale GetPrivateProfileString, sino GetPrivateProfileSection:

moradazo: siguiendo el ejemplo que puse anteriormente, para leer el archivo:
Aquí listamos todos los nombres de la sección "Usuarios" en un control ListBox.

Código:
<Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileSection(ByVal lpAppName As String, _
                                                        ByVal lpReturnedString As IntPtr, _
                                                        ByVal nSize As Integer, _
                                                        ByVal lpFileName As String) As Short
    End Function
    
    Private Sub btnListar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListar.Click
        'creamos la cadena de caracteres
        Dim sb As New System.Text.StringBuilder(255)
        ' asignamos la memoria al puntero
        Dim ip As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(255)
        Try
            Dim bt As Byte = GetPrivateProfileSection("Usuarios", ip, 255, "C:\Temp.ini")

            For i As Integer = 0 To bt - 1
                ' convertimos los bytes a caracteres y los añadimos al StringBuilder
                sb.Append(Convert.ToChar(Runtime.InteropServices.Marshal.ReadByte(ip, i)))
            Next
            ' eliminamos el último carácter que es nulo
            sb.Remove(sb.Length - 1, 1)
            ' limpiamos el TextBox y añadimos cada línea de la sección [Usuarios]
            ListBox1.Items.Clear()
            For Each line As String In sb.ToString().Split(Convert.ToChar(0))
                ListBox1.Items.Add(line)
            Next
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            ' liberamos la memoria
            Runtime.InteropServices.Marshal.FreeHGlobal(ip)
        End Try
    End Sub