Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static bool addPessoa(string path, pessoa p)//Adicionar pessoa
- {
- //verifica se existe senão cria o arquivo
- if (!System.IO.File.Exists(path))
- {
- System.IO.File.Create(path);
- }
- //cria bkp temporario
- System.IO.File.Copy(path, path + "_", true);
- //le o arquivo e adiciona a nova pessoa
- string dataJson = System.IO.File.ReadAllText(path, Encoding.UTF8);
- List<pessoa> retorno = JsonConvert.DeserializeObject<List<pessoa>>(dataJson);
- if (retorno == null)
- {
- retorno = new List<pessoa>();
- }
- retorno.Add(p);
- //salva o original
- var data = JsonConvert.SerializeObject(retorno);
- System.IO.StreamWriter writerJson = System.IO.File.CreateText(path);
- writerJson.WriteLine(data);
- writerJson.Flush();
- writerJson.Dispose();
- //deleta temporario
- System.IO.File.Delete(path + "_");
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement