Bonjour à tous,

Pour découvrir le C# et m'y habituer, j'essaie de créer un petit utilitaire qui tourne autour d'un fichier INI.

Dans ce fichier :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
[MaSection]
Key1 = "Ma valeur 1"
Key2 = "Ma valeur 2"
Key3 = "Ma valeur 3"
J'arrive à éditer cette section (mais pas encore à supprimer une valeur précise, je garde ça pour la fin) et à rajouter des nouvelles entrèes, seulement je bute un peu sur le fait de charger la section entière comme items d'une combobox.

Au début de mon code j'ai donc ceci :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
private ComboBox comboBox1;
 
public frmHome()
        {
            this.InitializeComponent();
            ///
            /// initialise la combo
            /// 
            cIni ini = new cIni(@".\test.ini");
            // le code erroné
            this.comboBox1.Items = ini.ReadValues("MaSection");
        }
Evidemment, Visual Studio m'insulte copieusement car j'utilise ce sample trouvé sur internet :

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
 
namespace MonSoft
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
 
    internal class cIni
    {
        private int li_BufferLen = 0x100;
        private string ls_IniFilename;
 
        public cIni(string pIniFilename)
        {
            this.ls_IniFilename = pIniFilename;
        }
 
        [DllImport("kernel32", SetLastError=true)]
        private static extern int GetPrivateProfileString(string pSection, string pKey, string pDefault, byte[] prReturn, int pBufferLen, string pFile);
        [DllImport("kernel32", SetLastError=true)]
        private static extern int GetPrivateProfileStruct(string pSection, string pKey, byte[] prReturn, int pBufferLen, string pFile);
        public void ReadSections(ref Array pSections)
        {
            pSections = this.z_GetString(null, null, null).Split(new char[1]);
        }
 
        public string ReadValue(string pSection, string pKey)
        {
            return this.z_GetString(pSection, pKey, "");
        }
 
        public string ReadValue(string pSection, string pKey, string pDefault)
        {
            return this.z_GetString(pSection, pKey, pDefault);
        }
 
        public void ReadValues(string pSection, ref Array pValues)
        {
            pValues = this.z_GetString(pSection, null, null).Split(new char[1]);
        }
 
        public void RemoveSection(string pSection)
        {
            WritePrivateProfileString(pSection, null, null, this.ls_IniFilename);
        }
 
        public void RemoveValue(string pSection, string pKey)
        {
            WritePrivateProfileString(pSection, pKey, null, this.ls_IniFilename);
        }
 
//----------------- Ici le "ReadSection" manquant que je n'arrive pas à coder
 
        [DllImport("kernel32", SetLastError=true)]
        private static extern int WritePrivateProfileString(string pSection, string pKey, string pValue, string pFile);
        [DllImport("kernel32", SetLastError=true)]
        private static extern int WritePrivateProfileStruct(string pSection, string pKey, string pValue, int pValueLen, string pFile);
        public void WriteValue(string pSection, string pKey, string pValue)
        {
            WritePrivateProfileString(pSection, pKey, pValue, this.ls_IniFilename);
        }
 
        private string z_GetString(string pSection, string pKey, string pDefault)
        {
            string str = pDefault;
            byte[] prReturn = new byte[this.li_BufferLen];
            int count = GetPrivateProfileString(pSection, pKey, pDefault, prReturn, this.li_BufferLen, this.ls_IniFilename);
            return Encoding.GetEncoding(0x4e4).GetString(prReturn, 0, count).TrimEnd(new char[1]);
        }
 
        public int BufferLen
        {
            get
            {
                return this.li_BufferLen;
N'arrivant pas à utiliser correctement le "ReadValues", je me suis dit qu'il était plus judicieux de crèer un ReadSection spécifique à l'utilisation pour une combo... seulement je cale méchemment après plusieurs essais infructueux.

Si quelqu'un pouvait m'aiguiller, ce serait très sympa car je ne sais pas où orienter mes recherches : kernel32, vérifier si ce cIni.cs est vraiment nécessaire ou pas ...

Bonne journée !