IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

problème avec un fichier .INI


Sujet :

VB.NET

  1. #1
    Membre du Club
    Inscrit en
    Mai 2007
    Messages
    102
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 102
    Points : 56
    Points
    56
    Par défaut problème avec un fichier .INI
    bonjour,

    J'ai un problème dans mon appli.
    Je veux initialiser mes variables grace a un fichier ini pour une meilleure flexibilité.

    J'ai configurer mon programme avec ce code suivant pour initialiser mes variables.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Short, ByVal lpFileName As String) As Integer
        Declare Function GetPrivateProfileInt Lib "Kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
     
     
        Function Get_Private_Profile_Int(ByVal cAppName As String, ByVal cKeyName As String, ByVal nKeyDefault As Integer, ByVal cProfName As String) As Long
     
     
            ' LIRE UN ENTIER
            ' Parametres:
            ' cAppName Correspond à [Rubrique]
            ' cKeyName Nom de l'entrée, de la clé
            ' nKeyDefault Valeur par défaut de la chaîne cherchée
            ' cProfName Nom du Fichier "INI" Privé
            ' Sortie:
            ' La fonction retourne une valeur numérique entière
     
            Get_Private_Profile_Int = GetPrivateProfileInt(cAppName, cKeyName, nKeyDefault, cProfName)
     
        End Function
     
        Function Get_Private_Profile_String(ByVal cAppName As String, ByVal cKeyName As String, ByVal cKeyDefault As String, ByRef cKeyValue As String, ByVal cProfName As String) As Integer
     
            ' LIRE UNE STRING
            ' Parametres:
            ' cAppName Correspond à [Rubrique]
            ' cKeyName Nom de l'entrée, de la clé
            ' cKeyDefault Valeur par défaut de la chaîne cherchée
            ' cKeyValue Valeur lue en face de l'Entrée ou cKeyDefault si l'Entrée est vide
            ' cProfName Nom du Fichier "INI" Privé
            ' Sortie:
            ' Valeur lue dans cKeyValue
            ' La fonction retourne le nombre de caractères dans cKeyValue 
     
            Dim iReaded As Integer
            Const sLongueur As Short = 255
            If cKeyName = "" Then
                cKeyValue = Space$(1025)
                iReaded = GetPrivateProfileString(cAppName, "", "", cKeyValue, 1024, cProfName)
            Else
                cKeyValue = Space$(255)
                iReaded = GetPrivateProfileString(cAppName, cKeyName, cKeyDefault, cKeyValue, sLongueur, cProfName)
            End If
            cKeyValue = Trim$(cKeyValue)
            'Enlever le dernier caractère?
            'If Len(cKeyValue) <> 0 Then
            ' cKeyValue = Mid$(cKeyValue, 1, Len(cKeyValue) - 1)
            'End If
            Get_Private_Profile_String = iReaded
     
        End Function
    ensuite je vais dans la partie Load de mon programme ou se trouve mes variable.

    Pour configurer ma varible je fais ce code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    RS232.BaudRate = Get_Private_Profile_Int("Config Port", "baudrate", 9600, "C:\scanning\bin\Test_RS232.ini")
    J'ai compiler pour voir si mon affectation avait marché et la mon programme sort une exception :



    C'est la première que j'utilise l'initialisation avec un fichier .ini donc si vous pouvez m'expliquez pourquoi j'ai cette exception, ca serait cool

    Merci d'avance

  2. #2
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 177
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 177
    Points : 25 125
    Points
    25 125
    Par défaut
    utiliser une dll pour lire un fichier ini

    regarde plutot les paramètres d'application

    sinon tu peux gérer un fichier ini par toi meme, il y a des classes dans system.io pour lire et écrire dans un fichier

  3. #3
    Membre du Club
    Inscrit en
    Mai 2007
    Messages
    102
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 102
    Points : 56
    Points
    56
    Par défaut
    salut,

    Désolé opur ma réponse tardive.

    Peut tu me dire le nom de la classe dans system.io qui permet de lire dans un fichier .ini stp, je trouve pas som nom.

    Merci d'avance

  4. #4
    Membre actif Avatar de tssi555
    Analyse système
    Inscrit en
    Juillet 2008
    Messages
    594
    Détails du profil
    Informations professionnelles :
    Activité : Analyse système

    Informations forums :
    Inscription : Juillet 2008
    Messages : 594
    Points : 295
    Points
    295
    Par défaut
    Salut azrael,


    j'éspère que tu trouvera ton bonheur ici :

    http://www.developpez.net/forums/d61...-correctement/



  5. #5
    Modérateur
    Avatar de sevyc64
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2007
    Messages
    10 223
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 223
    Points : 28 213
    Points
    28 213
    Par défaut
    En .Net, attention aux déclarations des API Windows, il faut les modifiées.

    Celles concernant les fichiers INI sont les suivantes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
      <DllImport("KERNEL32.DLL", EntryPoint:="GetPrivateProfileIntA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Private Shared Function GetPrivateProfileInt(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
      End Function
      <DllImport("KERNEL32.DLL", EntryPoint:="WritePrivateProfileStringA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Private Shared Function WritePrivateProfileString(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
      End Function
      <DllImport("KERNEL32.DLL", EntryPoint:="GetPrivateProfileStringA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Private Shared Function GetPrivateProfileString(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
      End Function
      <DllImport("KERNEL32.DLL", EntryPoint:="WritePrivateProfileStructA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Private Shared Function WritePrivateProfileStruct(ByVal lpszSection As String, ByVal lpszKey As String, ByVal lpStruct() As Byte, ByVal uSizeStruct As Integer, ByVal szFile As String) As Integer
      End Function
      <DllImport("KERNEL32.DLL", EntryPoint:="GetPrivateProfileStructA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Private Shared Function GetPrivateProfileStruct(ByVal lpszSection As String, ByVal lpszKey As String, ByVal lpStruct() As Byte, ByVal uSizeStruct As Integer, ByVal szFile As String) As Integer
      End Function
      <DllImport("KERNEL32.DLL", EntryPoint:="GetPrivateProfileSectionNamesA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Private Shared Function GetPrivateProfileSectionNames(ByVal lpszReturnBuffer() As Byte, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
      End Function
      <DllImport("KERNEL32.DLL", EntryPoint:="WritePrivateProfileSectionA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Private Shared Function WritePrivateProfileSection(ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
      End Function

  6. #6
    Membre du Club
    Inscrit en
    Mai 2007
    Messages
    102
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 102
    Points : 56
    Points
    56
    Par défaut
    rebonjour,

    merci tssi555 j'ai suivi ton post et j'ai trouvé mon bonheur
    j'arrive bien a utiliser le fichier .ini avec la méthode de Te-Deum

    Merci pour vos réponse.

  7. #7
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 177
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 177
    Points : 25 125
    Points
    25 125
    Par défaut
    mouais

    enfin y a moyen de faire la meme chose avec moins de code en utilisant un xmlserializer
    ca évite de réinventer la roue et ca permet aussi de sauvegarder autre chose que des types simple

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec le fichier Desktop.ini
    Par alainyvan dans le forum Windows 7
    Réponses: 2
    Dernier message: 02/02/2014, 21h35
  2. problème avec un fichier ini
    Par pasqual dans le forum C++Builder
    Réponses: 8
    Dernier message: 30/04/2007, 17h23
  3. Problème d'identificateur fichier ini
    Par yanba dans le forum Langage
    Réponses: 7
    Dernier message: 19/02/2006, 00h21
  4. Problème avec des fichiers
    Par Stany dans le forum C++
    Réponses: 7
    Dernier message: 17/10/2005, 16h53
  5. Problème avec les fichiers .JPG
    Par cprogil dans le forum Langage
    Réponses: 5
    Dernier message: 10/06/2003, 15h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo