1using System.Reflection;
20 public T
Read<T>(
string folderPath,
string fileName)
22 var path = Path.Combine(folderPath, fileName);
23 if (File.Exists(path))
25 var json = File.ReadAllText(path);
26 return JsonConvert.DeserializeObject<T>(json);
41 public void Save<T>(
string folderPath,
string fileName, T content)
43 if (!Directory.Exists(folderPath))
45 Directory.CreateDirectory(folderPath);
48 var fileContent = JsonConvert.SerializeObject(content, Formatting.Indented);
49 File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, Encoding.UTF8);
59 public void Delete(
string folderPath,
string fileName)
61 if (fileName !=
null && File.Exists(Path.Combine(folderPath, fileName)))
63 File.Delete(Path.Combine(folderPath, fileName));
83 onProgress?.Invoke(
this, 0);
84 List<string> lines =
new List<string>();
86 List<PropertyInfo> props = typeof(T).GetProperties().ToList();
90 List<string> originalHeaders = props.Select(p => p.Name).ToList();
91 List<string> formatedHeaders = props.Select(p => formatDataHeader ==
null ? p.Name : formatDataHeader(p.Name, p.PropertyType)).ToList();
93 lines.Add(
string.Join(delimiter.ToString(), formatedHeaders));
95 int processedElementsCnt = 0;
96 foreach (T data
in dataList)
98 string line =
string.Empty;
99 foreach (
string header
in originalHeaders)
101 PropertyInfo propInfo = typeof(T).GetProperty(header);
102 object dataObj = propInfo?.GetValue(data,
null);
103 if(formatData !=
null)
105 line += formatData(dataObj, data, propInfo);
113 line = line.Trim(delimiter);
115 cancellationToken.ThrowIfCancellationRequested();
118 if ((processedElementsCnt + 1) < dataList.Count)
120 onProgress?.Invoke(
this, (processedElementsCnt++ / (
float)dataList.Count) * 100);
124 File.WriteAllLines(filePath, lines.ToArray(), Encoding.UTF8);
125 onProgress?.Invoke(
this, 100);
143 List<T> dataList =
new List<T>();
144 if (!File.Exists(filePath)) {
return dataList; }
146 onProgress?.Invoke(
this, 0);
148 List<string> lines = File.ReadAllLines(filePath, Encoding.UTF8).ToList();
149 if (lines.Count >= 2)
151 List<string> headers = lines.First().Split(delimiter).ToList();
154 int processedElementsCnt = 0;
155 foreach (
string line
in lines)
157 List<string> lineParts = line.Split(delimiter).ToList();
159 for (
int i = 0; i < Math.Min(headers.Count, lineParts.Count); i++)
161 string header = findPropertyFromHeader ==
null ? headers[i] : findPropertyFromHeader(headers[i]);
162 setPropertyFromStringDelegate(data, header, lineParts[i]);
163 cancellationToken.ThrowIfCancellationRequested();
167 onProgress?.Invoke(
this, (processedElementsCnt++ / (
float)lines.Count) * 100);
170 onProgress?.Invoke(
this, 100);
Use this attribute to decorate elements that should not be saved to a file.
Use this attribute to order some elements to their position in the file.
int Order
Order of the property in the file.
Service that handles file operations.
void SaveToCsv< T >(string filePath, List< T > dataList, CancellationToken cancellationToken, ProgressDelegate onProgress=null, FormatDataDelegate formatData=null, FormatDataHeaderDelegate formatDataHeader=null, char delimiter=';')
Save the given data list to a .csv file.
void Save< T >(string folderPath, string fileName, T content)
Save the given object to a file.
List< T > LoadFromCsv< T >(string filePath, CancellationToken cancellationToken, SetPropertyFromStringDelegate< T > setPropertyFromStringDelegate, ProgressDelegate onProgress=null, FindPropertyFromHeaderDelegate findPropertyFromHeader=null, char delimiter=';')
Load the .csv file to a list of data.
void Delete(string folderPath, string fileName)
Delete the given file.
T Read< T >(string folderPath, string fileName)
Read the given file and deserialize it to the given type.
Interface for a service that handles file operations.
delegate void SetPropertyFromStringDelegate< T >(T dataObj, string propertyName, string value)
Delegate to change a specific property in the data object.
delegate void ProgressDelegate(object sender, float progress, string currentStep="")
Delegate void for progress changes.
delegate string FormatDataHeaderDelegate(string header, Type type)
Delegate to format data headers.
delegate string FindPropertyFromHeaderDelegate(string header)
Delegate to find propety name by localized header string.
delegate string FormatDataDelegate(object data, object parentObject, PropertyInfo currentProperty)
Delegate to format data.