-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdExistenceChecker_BeforeRefactoring.cs
More file actions
49 lines (44 loc) · 1.26 KB
/
IdExistenceChecker_BeforeRefactoring.cs
File metadata and controls
49 lines (44 loc) · 1.26 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
using System.Text.Json;
public class FileManager
{
public void IdFinder(string name, string dir, string ex, int id)
{
var file = dir + "/" + name + "." + ex;
if(!File.Exists(file))
{
Console.WriteLine("File does not exist under path: " + file);
return;
}
List<int> numbers = new List<int>();
if(ex == "txt")
{
//read the IDs from TXT file
var txt = File.ReadAllText(file);
var ids = txt.Split(',');
foreach(var fileId in ids)
{
numbers.Add(int.Parse(fileId));
}
}
else if(ex == "json")
{
//read the IDs from JSON file
var txt = File.ReadAllText(file);
numbers = JsonSerializer.Deserialize<List<int>>(txt);
}
else
{
Console.WriteLine("Unsupported file extension: " + ex);
return;
}
foreach(var number in numbers)
{
if(number == id)
{
Console.WriteLine($"Id {id} has been found in the file {file}.");
return;
}
}
Console.WriteLine($"Id {id} has not been found in the file {file}.");
}
}