Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Windows;
- namespace exercise_5
- {
- class ReadWriteCSV : ReadWrite
- {
- public new List<string[]> ReadFile(string filename)
- {
- List<string[]> content = new List<string[]>();
- StreamReader reader = File.OpenText(filename);
- string row = "";
- string[] data;
- string[] separatingChars = { ", " };
- //Loop to read the entire text
- while (row!= null)
- {
- row=reader.ReadLine();
- if (row != null && row.Length>0)
- {
- data = row.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);
- content.Add(data);
- }
- }
- reader.Close();
- return content;
- }
- public void WriteFile(string filename, List<string[]> data, bool append)
- {
- try
- {
- StreamWriter file = new StreamWriter(filename, append);
- using (file)
- {
- int count = 0;
- StringBuilder sb = new StringBuilder();
- foreach (var group in data)
- {
- foreach (var item in group)
- {
- if (item.Length > 0)
- {
- sb.Append(item);
- }
- if (count >= item.Length)
- {
- count = 0;
- }
- if (count++ <= item.Length)
- {
- sb.Append(", ");
- }
- }
- sb.AppendLine();
- count = 0;
- }
- file.WriteLine(sb.ToString());
- }
- }
- catch (Exception e)
- {
- MessageBox.Show((e.ToString()), "There was an error at WriteFile");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement