Advertisement
batoreiro

CodigoAjuda

Aug 29th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. public static bool addPessoa(string path, pessoa p)//Adicionar pessoa
  2. {
  3. //verifica se existe senão cria o arquivo
  4. if (!System.IO.File.Exists(path))
  5. {
  6. System.IO.File.Create(path);
  7. }
  8. //cria bkp temporario
  9. System.IO.File.Copy(path, path + "_", true);
  10. //le o arquivo e adiciona a nova pessoa
  11. string dataJson = System.IO.File.ReadAllText(path, Encoding.UTF8);
  12. List<pessoa> retorno = JsonConvert.DeserializeObject<List<pessoa>>(dataJson);
  13. if (retorno == null)
  14. {
  15. retorno = new List<pessoa>();
  16. }
  17. retorno.Add(p);
  18. //salva o original
  19. var data = JsonConvert.SerializeObject(retorno);
  20. System.IO.StreamWriter writerJson = System.IO.File.CreateText(path);
  21. writerJson.WriteLine(data);
  22. writerJson.Flush();
  23. writerJson.Dispose();
  24. //deleta temporario
  25. System.IO.File.Delete(path + "_");
  26. return true;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement