//Classe com rotinas Write e Read //------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.IO; using System.Configuration; namespace Helpers { public static class INI { [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName); public static string LerINI(string fileNameAndPath, string key) { if (System.IO.File.Exists(@fileNameAndPath) == false) return ""; string[] texto = File.ReadAllLines(@fileNameAndPath); for (int linhas = 0; linhas < texto.Length; linhas++) { if (texto[linhas].Trim() == key && texto.Length - 1 > linhas) return texto[linhas + 1].Trim(); } return string.Empty; } public static void WriteINI(string fileNameAndPath, string section, string key, string text) { WritePrivateProfileString(section, key, text, fileNameAndPath); } } } //Utilizando //----------- private void Form1_Load(object sender, EventArgs e) { string _status = string.Empty; //Leitura _status = Helpers.INI.LerINI(@"c:\temp\arquivo.ini","[STATUS_ATUALIZACAO]"); //Gravação Helpers.INI.WriteINI(@"c:\temp\arquivo.ini","STATUS_ATUALIZACAO","STATUS","AGUARDANDO"); }
Muito bom...
OLA Jose Barreto, eu nao entendi muito bem o que precisa, seria possivel postar um conteudo parecido com o teu ini e descrever o que precisa?
Bom dia, cara no seu codigo voce pega somente uma unica linha por vez, pegando a linha pelo linha + algum numero. Se eu quiser ler ate chegar ao próximo bloco [] do .ini como eu faço? no meu caso vou usar o .ini para pega uma conexão de dados inteira e setar no meu app.config
Opa, que bom que gostou. Se tiver algum script que queira compartilhar, se cadastre no site e compartilhe.
cara muito bom... valeu