Hoje mostro como criar arquivos .ini para armazenar configurações do seu software.
[sections] [section title=”Como criar arquivos .ini no C#”] No exemplo a seguir verifica se a pasta existe, se não cria a pasta.
Nesse exemplo será criada a pasta onde estiver instalado o executável da aplicação.

public class IniFile
{
public string Path;

public IniFile(string iniPath)
{
Path = iniPath;
}

public void WriteValue(string section, string key, string value)
{
NativeMethods.WritePrivateProfileString(section, key, value, Path);
}

public string ReadValue(string section, string key, string Default)
{
var buffer = new StringBuilder(255);
NativeMethods.GetPrivateProfileString(section, key, Default, buffer, 255, Path);

return buffer.ToString();
}

public void WriteValue(string section, string key, int value)
{
NativeMethods.WritePrivateProfileString(section, key, value.ToString(CultureInfo.InvariantCulture), Path);
}

public int ReadValue(string section, string key, int Default)
{
var buffer = new StringBuilder(255);
NativeMethods.GetPrivateProfileString(section, key, Default.ToString(CultureInfo.InvariantCulture), buffer, 255, Path);

return int.Parse(buffer.ToString());
}

public void WriteValue(string section, string key, UInt16 value)
{
NativeMethods.WritePrivateProfileString(section, key, value.ToString(CultureInfo.InvariantCulture), Path);
}
public UInt16 ReadValue(string section, string key, UInt16 Default)
{
var buffer = new StringBuilder(255);
NativeMethods.GetPrivateProfileString(section, key, Default.ToString(CultureInfo.InvariantCulture), buffer, 255, Path);

return UInt16.Parse(buffer.ToString());
}
public void WriteValue(string section, string key, UInt32 value)
{
NativeMethods.WritePrivateProfileString(section, key, value.ToString(CultureInfo.InvariantCulture), Path);
}
public UInt32 ReadValue(string section, string key, UInt32 Default)
{
var buffer = new StringBuilder(255);
NativeMethods.GetPrivateProfileString(section, key, Default.ToString(CultureInfo.InvariantCulture), buffer, 255, Path);

return UInt32.Parse(buffer.ToString());
}

public bool ReadValue(string section, string key, bool Default)
{
var buffer = new StringBuilder(255);
NativeMethods.GetPrivateProfileString(section, key, Default.ToString(CultureInfo.InvariantCulture), buffer, 255, Path);

return (buffer.ToString() != "False");
}
public void WriteValue(string section, string key, bool value)
{
NativeMethods.WritePrivateProfileString(section, key, value.ToString(CultureInfo.InvariantCulture), Path);
}
}

static class NativeMethods
{
[DllImport("kernel32", CharSet = CharSet.Unicode)] public static extern uint WritePrivateProfileString(string section,
string key, string val, string filePath);

[DllImport("kernel32", CharSet = CharSet.Unicode)] public static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal, int size,
string filePath);
}

Para gravar:

var ini = new IniFile(Application.StartupPath + "\\configuracao.ini");
ini.WriteValue("Grupo", "Dado", Valor_a_Gravar);

Para ler:

var ini = new IniFile(Application.StartupPath + "\\configuracao.ini");
var leitura = ini.ReadValue("Grupo", "Dado", ValorPadrao);

O valor padrão será utilizado caso não seja encontrado o valor solicitado.

[/section] [/sections]
Obrigado e até o próximo post.

DATEK Tecnologia Eletrônica
[C#] Como criar arquivos .ini no C#
Tags:                         

Deixe uma resposta

%d blogueiros gostam disto: