how to count records of an ini file in C#?

Asked

Viewed 146 times

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.

File Settings.ini

[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;
    }
}

}

  • What’s the problem? What exactly are you trying to do?

  • and that I don’t know how to do that I already searched and I don’t know how to count the names of an ini file or the addresses and put them separately in a loop for (int i = 0; i < 15; i++) in 15 for example that’s the amount I have of names in aqruivo and leave it automatic.

  • 2

    You are trying to count how many elements there are in a file . ini, correct?

  • yes I am tempted but I can’t imagine how ! already search only found to read and write but to read quantity of records .. nothing

1 answer

2

This is the simplest way to count how many elements and how many sections there 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;
}

Note that these methods should be implemented in the class IniFile.

  • Countsections() Not all code Path Return a value this was the error I received

  • I forgot to put the returns, the code has been updated with the fix.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.