Last active
March 18, 2025 14:15
-
-
Save pedroinfo/624b2bc797706f30cc207b6c83b19b46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConfigReader | |
{ | |
private readonly string _configFilePath; | |
// Constructor that initializes the config file path | |
public ConfigReader(string configFilePath = "appsettings.json") | |
{ | |
_configFilePath = configFilePath; | |
} | |
/// <summary> | |
/// Reads a key from the appsettings.json file. | |
/// </summary> | |
/// <param name="key">The key to search for in appsettings.json.</param> | |
/// <returns>The value of the key from appsettings.json or null if not found.</returns> | |
public string GetAppSetting(string key) | |
{ | |
// Check if the file exists | |
if (!File.Exists(_configFilePath)) | |
{ | |
return null; // Return null if the file is not found | |
} | |
try | |
{ | |
// Read the content of the appsettings.json file | |
var jsonContent = File.ReadAllText(_configFilePath); | |
// Deserialize the JSON content into a dictionary | |
var jsonObject = DeserializeJson<Dictionary<string, object>>(jsonContent); | |
// Check if the key exists and return the value | |
if (jsonObject.ContainsKey(key)) | |
{ | |
return jsonObject[key]?.ToString(); | |
} | |
} | |
catch (Exception) | |
{ | |
return null; // Return null if an error occurs during reading or deserialization | |
} | |
return null; // Return null if the key is not found | |
} | |
/// <summary> | |
/// Reads a connection string from the appsettings.json file. | |
/// </summary> | |
/// <param name="name">The name of the connection string to retrieve.</param> | |
/// <returns>The connection string value or null if not found.</returns> | |
public string GetConnectionString(string name) | |
{ | |
// Check if the file exists | |
if (!File.Exists(_configFilePath)) | |
{ | |
return null; // Return null if the file is not found | |
} | |
try | |
{ | |
// Read the content of the appsettings.json file | |
var jsonContent = File.ReadAllText(_configFilePath); | |
// Deserialize the JSON content into a dictionary | |
var jsonObject = DeserializeJson<Dictionary<string, object>>(jsonContent); | |
// Check if the "ConnectionStrings" section exists | |
if (jsonObject.ContainsKey("ConnectionStrings")) | |
{ | |
var connectionStrings = jsonObject["ConnectionStrings"] as Dictionary<string, object>; | |
// If connection strings exist, try to retrieve the connection string by name | |
if (connectionStrings != null && connectionStrings.ContainsKey(name)) | |
{ | |
return connectionStrings[name]?.ToString(); | |
} | |
} | |
} | |
catch (Exception) | |
{ | |
return null; // Return null if an error occurs during reading or deserialization | |
} | |
return null; // Return null if the connection string is not found | |
} | |
/// <summary> | |
/// Helper method to deserialize JSON content into a specified object type. | |
/// </summary> | |
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam> | |
/// <param name="jsonContent">The JSON content as a string.</param> | |
/// <returns>The deserialized object.</returns> | |
private T DeserializeJson<T>(string jsonContent) | |
{ | |
using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent))) | |
{ | |
var serializer = new DataContractJsonSerializer(typeof(T)); | |
return (T)serializer.ReadObject(memoryStream); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment