-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkingWithFile.cs
More file actions
34 lines (28 loc) · 1.01 KB
/
WorkingWithFile.cs
File metadata and controls
34 lines (28 loc) · 1.01 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
using System.Text.Json;
namespace BotProject
{
internal class WorkingWithFile
{
public static async Task WriteToFileAsync(List<TelegramUser> users)
{
using (StreamWriter sw = new StreamWriter(@"D:\C#\BotProject\users.json"))
{
JsonSerializerOptions options = new JsonSerializerOptions()
{
WriteIndented = true
};
string jsonData = JsonSerializer.Serialize(users, typeof(List<TelegramUser>), options);
await sw.WriteAsync(jsonData);
}
}
public static async Task<List<TelegramUser>> ReadFromFile()
{
using (StreamReader streamReader = new StreamReader(@"D:\C#\BotProject\users.json"))
{
string jsonData = await streamReader.ReadToEndAsync();
List<TelegramUser> users = JsonSerializer.Deserialize<List<TelegramUser>>(jsonData);
return users;
}
}
}
}