Bonjour,
J'utilise une classe qui crée un fichier xml utilisé comme fichier de configuration .
Voici la class:
Je l'initialise ainsi :
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 Option Strict Off Option Explicit On Imports System.IO Imports System.Xml Imports System.Xml.Serialization Imports System.Text Public Class cMySettings Public PhoneCountryCodes() As String Private Shared XMLFilename As String Public Sub New() SetToDefault(Me) End Sub Public Sub New(FileName As String) XMLFilename = FileName If Path.GetExtension(XMLFilename).ToLower() <> "xml" Then XMLFilename = XMLFilename + ".xml" If File.Exists(XMLFilename) = False Then SerializeToXML(New cMySettings()) End Sub Public Sub New(ByRef Settings As cMySettings) Me.PhoneCountryCodes = Settings.PhoneCountryCodes End Sub Public Shared Sub SerializeToXML(ByRef Settings As cMySettings) Dim xmlSerializer As New XmlSerializer(GetType(cMySettings)) Using xmlTextWriter As New XmlTextWriter(XMLFilename, Encoding.UTF8) xmlTextWriter.Formatting = Formatting.Indented xmlSerializer.Serialize(xmlTextWriter, Settings) xmlTextWriter.Close() End Using End Sub Public Shared Sub DeseralizeFromXML(ByRef Settings As cMySettings) Dim fs As FileStream = Nothing ' do i have settings? If File.Exists(XMLFilename) = True Then Try fs = New FileStream(XMLFilename, FileMode.Open, FileAccess.Read) Dim xmlSerializer As New XmlSerializer(GetType(cMySettings)) Settings = xmlSerializer.Deserialize(fs) Catch 'load error of some sort, or OBJECT deserialize error 'do we tell anyone? Exit Sub Finally If Not fs Is Nothing Then fs.Close() fs = Nothing End Try End If End Sub Public Shared Sub Copy(ByRef SourceSettings As cMySettings, ByRef DestSettings As cMySettings) DestSettings.PhoneCountryCodes = SourceSettings.PhoneCountryCodes End Sub Public Shared Sub SetToDefault(ByRef Settings) Settings.PhoneCountryCodes = {"0", "33", "00"} End Sub Public Shared Function Compare(ByRef Settings1 As cMySettings, ByRef Setting2 As cMySettings) As Boolean If Settings1.PhoneCountryCodes IsNot Setting2.PhoneCountryCodes Then Compare = False : Exit Function 'If Settings1.PhoneCountryCodes(0) <> Setting2.PhoneCountryCodes(0) Then Compare = False : Exit Function 'If Settings1.PhoneCountryCodes(1) <> Setting2.PhoneCountryCodes(1) Then Compare = False : Exit Function 'If Settings1.PhoneCountryCodes(2) <> Setting2.PhoneCountryCodes(2) Then Compare = False : Exit Function Compare = True End Function End Class
Je veux modifier la valeur 33 par défaut !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Dim PluginSettings As cMySettings Dim TempPluginSettings As cMySettings ... ''''''''''''''''' ''' Settings ''' ''''''''''''''''' PluginSettings = New cMySettings(Path.Combine(pluginDataPath, "MobilePhone")) ' read in defaults cMySettings.DeseralizeFromXML(PluginSettings) ' copy to temp TempPluginSettings = New cMySettings(PluginSettings)
J'écrais donc:
Il semblerait que la fonction compare ne fonctionne pas avec des variables tableau !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 TempPluginSettings.PhoneCountryCodes(1) = "44" If cMySettings.Compare(PluginSettings, TempPluginSettings) = False Then cMySettings.Copy(TempPluginSettings, PluginSettings) cMySettings.SerializeToXML(PluginSettings) End If
Merci de votre aide !
Partager