Skip to content

Instantly share code, notes, and snippets.

@icalderond
Created December 26, 2014 23:33
Show Gist options
  • Save icalderond/239d91e6444edcf0ca95 to your computer and use it in GitHub Desktop.
Save icalderond/239d91e6444edcf0ca95 to your computer and use it in GitHub Desktop.
Leer archivo ini y convertirlo a objectos
public class Configuration
{
public Configuration(string _seccion)
{
this.Seccion = _seccion;
}
public string Seccion { get; set; }
private List<Parameter> parameters;
public List<Parameter> Parameters
{
get
{
if (parameters == null)
{
parameters = new List<Parameter>();
}
return parameters; }
set { parameters = value; }
}
}
public class Parameter
{
public string Name { get; set; }
public string Value { get; set; }
}
public class DataConfiguration
{
public List<Configuration> ValuesOfIniFile = new List<Configuration>();
public DataConfiguration(string pathIni)
{
FillValues(pathIni);
}
private void FillValues(string pathIni)
{
if (File.Exists(pathIni))
{
string[] lines = System.IO.File.ReadAllLines(pathIni);
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i].TrimEnd().TrimStart();
if (line != ""&&line[0] == '[')
{
var strop = lines[i].Replace("[", "").Replace("]", "");
Configuration config = new Configuration(strop);
for (i++; i < lines.Length; i++)
{
if (lines[i] != "" && lines[i][0] != '[')
{
string[] lineSplited = lines[i].TrimEnd().TrimStart().Split('=');
if (lineSplited.Length == 2)
{
string name = lineSplited[0];
string value = lineSplited[1];
config.Parameters.Add(new Parameter() { Name = name, Value = value });
}
else
{
config.Parameters.Add(new Parameter() { Value = lines[i] });
}
}
else
{
i--;
break;
}
}
ValuesOfIniFile.Add(config);
}
}
}
else
{
throw new DirectoryNotFoundException("El archivo especificado no existe");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment