Fichier de configuration

Résolu
AirByte Messages postés 13 Date d'inscription mercredi 5 janvier 2011 Statut Membre Dernière intervention 25 février 2011 - 8 févr. 2011 à 16:51
AirByte Messages postés 13 Date d'inscription mercredi 5 janvier 2011 Statut Membre Dernière intervention 25 février 2011 - 8 févr. 2011 à 21:17
Bonjour a tous

J'aurais besoin de conseils, j'ai une application qui nécessite un fichier de configuration modifiable dans le programme et autant que possible en dehors du programme. Je suis sous VB Studio 2010.

Ce que je cherche a gérer au fond c'est un .ini, mais il semble que ce type de fichier ai été abandonné?
Mon programme doit disposé d'une vingtaine de paramètre modifiable.

Quel serait la meilleur méthode?

Merci!

4 réponses

cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
8 févr. 2011 à 19:49
Comme tu risque de pas trouver les bonnes déclarations des api sur le net (elles sont généralement données pour VB6 pas pour .Net), je te les donne :

#Region "###### Déclarations des API Windows ##############################################"
  <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:="GetPrivateProfileSectionA", SetLastError:=False, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetPrivateProfileSection(ByVal lpAppName As String, 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

#End Region

Les principales sont GetPrivateProfileString et WritePrivateProfileString, pour respectivement, lire et écrire. Les autres déclarations sont pour les fonctionnalités avancées autour du format ini.

Exemple d'utilisation :
......
Dim sb As New StringBuilder(MAX_ENTRY)
Dim Ret As Integer = GetPrivateProfileString("NomSection", "NomClé", "ValeurParDefaut", sb, MAX_ENTRY, "CheminEtNomDuFichier")
Valeur = sb.ToString
......


......
If (WritePrivateProfileString("NomSection", "NomClé", "Valeur", "CheminEtNomDuFichier") <> 0) then
Messagebox.Show("Ecriture OK")
......


MAX_ENTRY est une constante fixée à 32768

ValeurParDefaut est la valeur qui sera retournée si la clé ou la section n'est pas trouvée. Souvent on indique une chaine vide.


[i][b]---- Sevyc64 (alias Casy) ----
[hr]# LE PARTAGE EST NOTRE FORCE #/b/i
3
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
8 févr. 2011 à 18:06
mais il semble que ce type de fichier ai été abandonné?

C'était effectivement le désirata de Microsoft à la sortie de Windows 95. Il devait, à l'époque être remplacé par la base de registre, et plus récemment (W2K & XP) par le format xml.

Force est de constater que le fichier ini existe toujours, les API windows permettant de les gérer sont toujours supportées au point que Microsoft lui-même utilise toujours ce format dans certains logiciels.


Concernant la méthode tu as donc le choix, fichier ini, fichier xml, base de registre, fichier de ton propre format.
Pour le premier, les api Windows sont relativement simple à mettre en œuvre. Pour les 2 suivants tout est déjà compris dans le framework pour les gérer.
Quant au dernier cas, tu as tout à inventer

Sans compter les solutions que j'ai pu oublier.


[i][b]---- Sevyc64 (alias Casy) ----
[hr]# LE PARTAGE EST NOTRE FORCE #/b/i
0
AirByte Messages postés 13 Date d'inscription mercredi 5 janvier 2011 Statut Membre Dernière intervention 25 février 2011
8 févr. 2011 à 18:42
Merci pour cette réponse Casy,

Je crois que je vais aller vers le .ini pour commencer. Je ne savais pas que c'était via les APIs, ça va faciliter mes recherches.

Par contre j'aurais aimé en savoir plus sur la méthode par fichiers XML, aurais-tu un bon tuto ou un site a me recommander?
0
AirByte Messages postés 13 Date d'inscription mercredi 5 janvier 2011 Statut Membre Dernière intervention 25 février 2011
8 févr. 2011 à 21:17
Rebonjour

Ça marche #1

J'ai du ajouter au début du code
Imports System.Text


Merci beaucoup!
0
Rejoignez-nous