Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Windows;
  8.  
  9. namespace exercise_5
  10. {
  11. class ReadWriteCSV : ReadWrite
  12. {
  13. public new List<string[]> ReadFile(string filename)
  14. {
  15. List<string[]> content = new List<string[]>();
  16. StreamReader reader = File.OpenText(filename);
  17. string row = "";
  18. string[] data;
  19. string[] separatingChars = { ", " };
  20.  
  21. //Loop to read the entire text
  22. while (row!= null)
  23. {
  24. row=reader.ReadLine();
  25. if (row != null && row.Length>0)
  26. {
  27. data = row.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);
  28. content.Add(data);
  29. }
  30. }
  31. reader.Close();
  32. return content;
  33. }
  34. public void WriteFile(string filename, List<string[]> data, bool append)
  35. {
  36. try
  37. {
  38. StreamWriter file = new StreamWriter(filename, append);
  39. using (file)
  40. {
  41. int count = 0;
  42. StringBuilder sb = new StringBuilder();
  43. foreach (var group in data)
  44. {
  45. foreach (var item in group)
  46. {
  47. if (item.Length > 0)
  48. {
  49. sb.Append(item);
  50. }
  51. if (count >= item.Length)
  52. {
  53. count = 0;
  54. }
  55. if (count++ <= item.Length)
  56. {
  57. sb.Append(", ");
  58. }
  59. }
  60. sb.AppendLine();
  61. count = 0;
  62. }
  63. file.WriteLine(sb.ToString());
  64. }
  65. }
  66. catch (Exception e)
  67. {
  68. MessageBox.Show((e.ToString()), "There was an error at WriteFile");
  69. }
  70. }
  71.  
  72.  
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement