Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Runtime.Serialization;
  11. using System.Runtime.Serialization.Json;
  12. using System.IO;
  13.  
  14. namespace app0._1
  15. {
  16.  
  17. class JsonRW
  18. {
  19.  
  20. //если папка Download содержит >=4 файлов, то используем файлы из этой папки
  21. static JsonRW()
  22. {
  23. string syncFilesPath = Sync.pathToDownloadFolder;
  24. string folder = "";
  25. if(Directory.GetFiles(syncFilesPath).Length >= 4)
  26. {
  27. folder = syncFilesPath;
  28. }
  29. CategoryPath = folder + @"category.json";
  30. CategoryRecipePath = folder + @"categoryrecipe.json";
  31. ProductPath = folder + @"product.json";
  32. RecipePath = folder + @"recipe.json";
  33. }
  34.  
  35. public static string CategoryPath = @"category.json", CategoryRecipePath = @"categoryrecipe.json",
  36. ProductPath = @"product.json", RecipePath = @"recipe.json";
  37. public static List<Category> readCategory()
  38. {
  39. var arr = new List<Category>();
  40. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<Category>));
  41. try
  42. {
  43. using (FileStream fs = new FileStream(CategoryPath, FileMode.OpenOrCreate))
  44. {
  45. arr = (List<Category>)jsonFormatter.ReadObject(fs);
  46. }
  47. }
  48. catch
  49. {
  50. arr = new List<Category>();
  51. }
  52. return arr;
  53. }
  54. public static bool writeCategory(List<Category> arr)
  55. {
  56. try
  57. {
  58. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<Category>));
  59. using (FileStream fs = new FileStream(CategoryPath, FileMode.OpenOrCreate))
  60. {
  61. jsonFormatter.WriteObject(fs, arr);
  62. }
  63. }
  64. catch
  65. {
  66. return false;
  67. }
  68. return true;
  69. }
  70.  
  71. public static List<CategoryRecipe> readCategoryRecipe()
  72. {
  73. var arr = new List<CategoryRecipe>();
  74. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<CategoryRecipe>));
  75. try
  76. {
  77. using (FileStream fs = new FileStream(CategoryRecipePath, FileMode.OpenOrCreate))
  78. {
  79. arr = (List<CategoryRecipe>)jsonFormatter.ReadObject(fs);
  80. }
  81. }
  82. catch
  83. {
  84. arr = new List<CategoryRecipe>();
  85. }
  86. return arr;
  87. }
  88. public static bool writeCategoryRecipe(List<CategoryRecipe> arr)
  89. {
  90. try
  91. {
  92. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<CategoryRecipe>));
  93. using (FileStream fs = new FileStream(CategoryRecipePath, FileMode.OpenOrCreate))
  94. {
  95. jsonFormatter.WriteObject(fs, arr);
  96. }
  97. }
  98. catch
  99. {
  100. return false;
  101. }
  102. return true;
  103. }
  104. public static List<Product> readProduct()
  105. {
  106. var arr = new List<Product>();
  107. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<Product>));
  108. try
  109. {
  110. using (FileStream fs = new FileStream(ProductPath, FileMode.OpenOrCreate))
  111. {
  112. arr = (List<Product>)jsonFormatter.ReadObject(fs);
  113. }
  114. }
  115. catch
  116. {
  117. arr = new List<Product>();
  118. }
  119. return arr;
  120. }
  121. public static bool writeProduct(List<Product> arr)
  122. {
  123. try
  124. {
  125. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<Product>));
  126. using (FileStream fs = new FileStream(ProductPath, FileMode.OpenOrCreate))
  127. {
  128. jsonFormatter.WriteObject(fs, arr);
  129. }
  130. }
  131. catch
  132. {
  133. return false;
  134. }
  135. return true;
  136. }
  137.  
  138.  
  139. public static List<Recipe> readRecipe()
  140. {
  141. var arr = new List<Recipe>();
  142. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<Recipe>));
  143. try
  144. {
  145. using (FileStream fs = new FileStream(RecipePath, FileMode.OpenOrCreate))
  146. {
  147. arr = (List<Recipe>)jsonFormatter.ReadObject(fs);
  148. }
  149. }
  150. catch
  151. {
  152. arr = new List<Recipe>();
  153. }
  154. return arr;
  155. }
  156. public static bool writeRecipe(List<Recipe> arr)
  157. {
  158. try
  159. {
  160. DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<Recipe>));
  161. using (FileStream fs = new FileStream(RecipePath, FileMode.OpenOrCreate))
  162. {
  163. jsonFormatter.WriteObject(fs, arr);
  164. }
  165. }
  166. catch
  167. {
  168. return false;
  169. }
  170. return true;
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement