public static List <string> ReadFromCsvFile(string path) { if (FileCreationHelper.ExistanceCheck(path)) { return(File.ReadAllLines(path).Skip(1).ToList()); } return(null); }
internal static List <string> ReadFromJsonFile(string inputPath) { if (FileCreationHelper.ExistanceCheck(inputPath)) { return((File.ReadAllLines(inputPath)).Skip(1).ToList()); } return(null); }
internal static string WriteToJson <T>(List <T> list, string outputPath) { var jsonString = JsonConvert.SerializeObject(list); FileCreationHelper.ExistanceCheckCreate(outputPath); using (var sw = new StreamWriter(File.Create(outputPath))) { sw.Write(jsonString); } return($"Exported {list.Count} as JSON to {outputPath}"); }
public static string WriteToCsv <T>(List <T> list, string path) { try { if (list == null || list.Count == 0) { return("No data to export"); } Type t = list[0].GetType(); string newLine = Environment.NewLine; FileCreationHelper.ExistanceCheckCreate(path); using (var sw = new StreamWriter(File.Create(path))) { object o = Activator.CreateInstance(t); PropertyInfo[] props = o.GetType().GetProperties(); sw.Write(string.Join(",", props.Select(d => d.Name).ToArray()) + newLine); list.ForEach(item => { var row = string.Join(",", props.Select(d => item.GetType() .GetProperty(d.Name) .GetValue(item, null) .ToString()) .ToArray()); sw.Write(row + newLine); }); } return($"Exported {list.Count} as CSV to {path}"); } catch (Exception ex) { var sb = new StringBuilder(); sb.Append("Thats not cool, something went wrong with the export:"); sb.Append(Environment.NewLine); sb.Append(ex.InnerException.ToString()); return(sb.ToString()); } }