how to count records from an ini file in C #? [closed]

0
 Como contar Registros de um Aqruivo.ini e usa-las em loop 

  for (int i = 0; i < 15; i++)

    {

  Exemplo: no [bot] tenho 5 elementos cadastrados ! ao invez de ser 15 
    adicionado manualmente ao for eu  usaria uma variavel no local -> que 
    daria o valor de 5 lidos automaticamente.

Settings.ini file

[bot]
b00=Debian
b01=Mineos
b02=Utorrent
b03=Debian Apache Web Server 
b04=Debian Mysql Web Server
b05=Ez Monitor


[web]
h00=https://debian:10100/
h01=https://debian:8443/
h02=https://debian:8080/gui/
h03=http://ccstudio.zapto.org/
h04=http://debian/phpmyadmin/
h05=http://debian/app/cc/linux/eZ/
h06=http://debian:19999

My Used Class

using System.IO;
 using System.Reflection;
using System.Runtime.InteropServices;
 using System.Text;

/ / Change this to match your program's normal namespace
    namespace MyProg
 {
class IniFile   // revision 11
{
    string Path;
    string EXE = Assembly.GetExecutingAssembly().GetName().Name;

    [DllImport("kernel32", CharSet = CharSet.Unicode)]
    static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

    [DllImport("kernel32", CharSet = CharSet.Unicode)]
    static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

    public IniFile(string IniPath = null)
    {
        Path = new FileInfo(IniPath ?? EXE + ".ini").FullName.ToString();
    }

    public string Read(string Key, string Section = null)
    {
        var RetVal = new StringBuilder(255);
        GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
        return RetVal.ToString();
    }

    public void Write(string Key, string Value, string Section = null)
    {
        WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
    }

    public void DeleteKey(string Key, string Section = null)
    {
        Write(Key, null, Section ?? EXE);
    }

    public void DeleteSection(string Section = null)
    {
        Write(null, null, Section ?? EXE);
    }

    public bool KeyExists(string Key, string Section = null)
    {
        return Read(Key, Section).Length > 0;
    }
}

}

    
asked by anonymous 02.08.2017 / 20:05

1 answer

2

This is the simplest way to count how many elements and how many sections are in the file:

// Obtém o conteúdo do arquivo .ini
public string GetFileFullText() => IO.File.ReadAllText(this.Path);
// Conta quantas seções há nele
public int CountSections() {
    string fileText = GetFileFullText();
    int count = fileText.Count(f => f == ']');
    return count;
}
// Conta quantos elementos (nomes) há no arquivo
public int CountKeys() {
    string fileText = GetFileFullText();
    int count = fileText.Count(f => f == '='); // Se o delimitador for um :, substitua ele ali
    return count;
}

Remembering that these methods should be implemented in class IniFile .

    
02.08.2017 / 22:11