There are several ways to implement this, for example json or text (* .txt).
Json
Install the Json.NET package with the package manager NuGet :
Aftersuchinstallationyoucanfromastructure,anobject,etc.generateajson
ofitandwritetoafilewiththeextension.json
Encoding:
Serialize
//objetocriadoList<string[]>objListString=newList<string[]>();objListString.Add(newstring[]{"1", "valor 1" });
objListString.Add(new string[] { "2", "valor 2" });
objListString.Add(new string[] { "3", "valor 3" });
//serializando objeto no formato Json
var data = JsonConvert.SerializeObject(objListString);
//gravando informação em um arquivo na pasta raiz do executavel
System.IO.StreamWriter writerJson = System.IO.File.CreateText(".\base.json");
writerJson.Write(data);
writerJson.Flush();
writerJson.Dispose();
After executing this code, you will have a file inside the \bin
folder named base.json in this format:
Deserialize
TodothereverseprocessjustreadthefileagainanduseJsonConvert.DeserializeObject
thisway:
StringdataJson=System.IO.File.ReadAllText(".\base.json", Encoding.UTF8);
List<string[]> retorno = JsonConvert.DeserializeObject<List<string[]>>(dataJson);
Result
Text
Coding
To generate the same example in the format of a .txt
file is simple, note code:
System.IO.StreamWriter writerTxt = System.IO.File.CreateText(".\base.txt");
foreach(string[] item in objListString.ToArray())
{
writerTxt.WriteLine(String.Join(";", item));
}
writerTxt.Flush();
writerTxt.Dispose();
Togeneratetheinversetxtprocessforanobjectinyourprogramdo
String[]dataTxt=System.IO.File.ReadAllLines(".\base.txt", Encoding.UTF8);
foreach (String linha in dataTxt)
{
String[] itens = linha.Split(';');
if (itens.Count() > 0)
{
objListString.Add(itens);
}
}
Bothwaysareviablealternatives,butifIweretochooseIwouldput json .