Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Leggere File .ini

  1. #1

    Leggere File .ini

    Ciao a tutti, sto scrivendo una applicazione web in asp.net (vb) e devo leggere dei dati da un file ini. I titoli delle sezioni riesco a leggerli tranquillamente con la funzione:
    ----
    Public Function SezioniIni(ByVal INIFile As String, Optional ByVal bufferSize As Integer = 16384) As String()
    Dim sb As New String(" "c, bufferSize)
    Dim ret As Integer
    ret = GetPrivateProfileSectionNames(sb, sb.Length, INIFile)
    Select Case ret
    Case 0
    Throw New ArgumentException("Invalid INI file.", "INIFile")
    Case sb.Length - 2
    Throw New ArgumentException("The buffer is too small.", "bufferSize")
    End Select
    sb = sb.TrimEnd(" "c).TrimEnd(ControlChars.NullChar)
    Return sb.Split(ControlChars.NullChar)
    End Function
    -----
    Mentre non riensco a leggere i valori delle chiavi.

    Con questa funzione mi solleva una eccezione:
    ----
    Public Function Read(ByVal Filename As String, ByVal Section As String, ByVal Key As String) As String
    Dim Result As String
    Dim RetVal As String = New String(" ", 255)
    Dim LenResult As Integer
    Dim ErrString As String
    LenResult = GetPrivateProfileString(Section, Key, "non trovato", RetVal, RetVal.Length, Filename)
    If LenResult = 0 Then
    If Not (File.Exists(Filename)) Then
    ErrString = "Impossibile trovare il file " & Filename
    Else
    ErrString = "Impossibile eseguire l'operazione: la sezione o la chiave sono errate oppure l'accesso al file non è consentito"
    End If
    Throw New IniException(ErrString)
    End If
    Result = RetVal.Substring(0, LenResult)
    Return Result
    End Function
    ---
    Mentre con questa non viene fuori niente:
    ---
    Public Function LeggiIni(ByVal File As String, ByVal Section As String, ByVal Item As String) As String
    Dim sBuf As String
    Dim iRC As Integer

    sBuf = Space(1023)
    iRC = GetPrivateProfileString(Section, Item, "Not Found", sBuf, sBuf.Length, File)
    sBuf = Left$(sBuf, iRC)

    If StrComp(sBuf, "Not Found") = 0 Then
    LeggiIni = vbNullString
    Else
    LeggiIni = Trim(sBuf.ToString)
    End If

    End Function
    -----

    per favore aiutatemi, sto impazzendo!!!

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    prova così:

    codice:
    Imports System
    Imports System.IO
    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Namespace Ini
        Public Class IniFile
            Private ReadOnly _path As String
            Public ReadOnly Property path() As String
                Get
                    Return Me._path
                End Get
            End Property
    
    
            <DllImport("kernel32")> _
            Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal val As String, ByVal filePath As String) As Long
            End Function
    
            <DllImport("kernel32")> _
            Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
            End Function
    
            Public Sub New(ByVal INIPath As String)
                Me._path = INIPath
            End Sub
    
            Public Sub IniWriteValue(ByVal Section As String, ByVal Key As String, ByVal Value As String)
                WritePrivateProfileString(Section, Key, Value, Me.path)
            End Sub
    
            Public Function IniReadValue(ByVal Section As String, ByVal Key As String) As String
                Dim temp As New StringBuilder(255)
                Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, Me.path)
                Return temp.ToString()
    
            End Function
        End Class
    End Namespace
    Pietro

  3. #3
    aleeeeee! Funzionaaaa!

    Ti ringrazio davvero tanto!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.