-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileReader.cs
More file actions
36 lines (30 loc) · 1.21 KB
/
FileReader.cs
File metadata and controls
36 lines (30 loc) · 1.21 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
namespace Common;
using System.IO;
public interface IFileReader
{
public StreamReader ReadStream(Stream stream);
}
public class FileReader : IFileReader
{
public StreamReader ReadStream(Stream stream)
{
IFileReader fileReader = new FileReader();
StreamReader reader = fileReader.ReadStream(stream);
return reader;
}
public static string ReadJsonFileFromPath(string fileName)
{
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string[] potentialDirectories =
{
Path.Combine(basePath, $"{fileName}"),
Path.Combine(basePath, "..", "..", $"{fileName}"),
Path.Combine(basePath, "..", "..", "..", "..", "..", "..", "TestUtils", "MockData", $"{fileName}"),
Path.Combine(basePath, "..", "..", "..", "..", "..", "..", "tests", "TestUtils", "MockData", $"{fileName}"),
Path.Combine(basePath, "TestUtils", "MockData", $"{fileName}"),
};
string filePath = potentialDirectories.Select(Path.GetFullPath).FirstOrDefault(File.Exists)
?? throw new FileNotFoundException($"File '{fileName}' not found in any of the specified locations.");
return File.ReadAllText(filePath);
}
}