forked from MicrosoftDocs/mslearn-dotnet-files
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
99 lines (72 loc) · 3.34 KB
/
Copy pathProgram.cs
File metadata and controls
99 lines (72 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
usingNewtonsoft.Json;
usingSystem.IO;
usingSystem.Collections.Generic;
// Use Directory.getCurrentDirectory() to get the current working directory
// Use Path.Combine(pathSegment_1, pathSegment_2...) to ensure the correct format for different OS
varcurrentDirectory=Directory.GetCurrentDirectory();
varstoresDirectory=Path.Combine(currentDirectory,"stores");
// Use Directory.CreateDirectory(path) to create a new directory
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(),"stores","205"));
// Use Directory.Exists(path) to check if a directory exists
booldoesDirectoryExist=Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(),"stores","205"));
// Use File.ReadAllText(filepath) to read the file content (return as string by default)
varsalesJson=File.ReadAllText($"stores{Path.DirectorySeparatorChar}201{Path.DirectorySeparatorChar}sales.json");
varsalesTotalDir=Path.Combine(Directory.GetCurrentDirectory(),"salesTotalDir");
Directory.CreateDirectory(salesTotalDir);
// add package Newtonsoft.Json locally, call the method Json.Convert.DeserializeObject<T>(JSON-like string)
varsalesData=JsonConvert.DeserializeObject<SalesTotal>(salesJson);
// Use File.WriteAllText(filepath, content<string>) to write to the file
// So you need to use ToString() to be able to print the content to File.WriteAllText()
File.WriteAllText($"salesTotalDir{Path.DirectorySeparatorChar}totals.txt",salesData.Total.ToString());
// Use Environment.NewLine to add a new line after content
// Use Path.DirectorySeparatorChar to ensure the correct format for different OS
File.WriteAllText($"salesTotalDir{Path.DirectorySeparatorChar}totals.txt",$"{salesData.Total}{Environment.NewLine}");
varsalesFiles=FindFiles(storesDirectory);
varsalesTotal=CalculateSalaesTotal(salesFiles);
// Use File.AppendAllText(filepath, content) to avoid overwrite the whole file with new content
File.AppendAllText(Path.Combine(salesTotalDir,"totals.txt"),$"{salesTotal}{Environment.NewLine}");
/*
* FindFiles take a directory name as argument
* create an Enumerable of string
* use Directory.EnumerateFiles(folderName, fileFormat, options)
* to search through the directory to get all files
* which fulfill conditions inside the directory
* and add them to the Enumerable as result
*/
IEnumerable<string>FindFiles(stringfolderName)
{
List<string>salesFiles=newList<string>();
varfoundFiles=Directory.EnumerateFiles(folderName,"*",SearchOption.AllDirectories);
foreach(varfileinfoundFiles)
{
varextension=Path.GetExtension(file);
if(extension==".json")
{
salesFiles.Add(file);
}
}
returnsalesFiles;
}
/*
* CalculateSalesTotal takes an Enumerable of string as argument
* Loop through the element(file) inside the Enumerable
* Read them one by one
* Parse the content to Json and get the number
* Sum up the number inside the file content
*/
doubleCalculateSalaesTotal(IEnumerable<string>salesFiles)
{
doublesalesTotal=0;
foreach(varfileinsalesFiles)
{
vardata=File.ReadAllText(file);
SalesData?salesStat=JsonConvert.DeserializeObject<SalesData?>(data);
salesTotal+=salesStat?.Total??0;
}
returnsalesTotal;
}
recordSalesData(doubleTotal);
classSalesTotal
{
publicdoubleTotal{get;set;}
}