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.

Arquivo 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

Minha Classe Usada

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

}

4
  • Onde está o problema? O que está tentando fazer, exatamente? 2/08/2017 às 18:18
  • e que eu nao sei fazer isso ja pesquisei e não sei contar os nomes de um arquivo ini ou os endereços e colocar separadamente em um loop for (int i = 0; i < 15; i++) no 15 por exemplo que eh a quantidade que tenho de nomes no aqruivo e deixa-lo automatico. 2/08/2017 às 18:25
  • 2
    Você está tentando contar quantos elementos há num arquivo .ini, correto? 2/08/2017 às 18:34
  • sim estou tentanto mas nao imagino como ! ja pesquiser só achei ler e escrever mas ler a quantidade de registros ..nada 2/08/2017 às 18:41

1 Resposta 1

2

Essa é a maneira mais simples de contar quantos elementos e quantas seções há no arquivo:

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

Lembrando, que estes métodos devem ser implementados na classe IniFile.

2
  • CountSections() NOt all code Path return a value este foi o erro q recebi 3/08/2017 às 0:32
  • Esqueci de colocar os retornos, o código foi atualizado com a correção. 3/08/2017 às 2:46

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .