I want to store some privacy info that is usually in my code to a local file on my pc, to make sure it won't be committed to version control. I want to read this file in my code into a dictionary directly.
Which file type is works to do this with? I thought about xlsx/csv or json, but I am not sure which is best for reading into a dictionary right away. By the way, I know how to read files with (for example) File.ReadAllText and stuff. It is just that this plain text does not compile easily to a dictionary. I am still free to choose any saving format I want, but here is an example of data
username1 - marja1
password1 - fakepassword
username2 - marja2
password2 - fake
Curious to any suggestions.
There are lots of options, each with their own pros and cons. Any standard format will be easy to deserialize from with a third-party library, so that doesn't differentiate them. But here's a comparison of what your file might look like with each.
Simple key-value pairs. XML wouldn't really work to directly translate a dictionary this way.
username1 = password1
username2 = password2
{
"username1":"password1",
"username2":"password2",
}
username1: password1
username2: password2
You may want a more advanced model, deserializable into something like the following C# types:
record FileContents(IEnumerable<UserCredential> Credentials);
record UserCredential(string UserName, string Password);
{
"credentials": [
{
"username": "username1",
"password": "password1"
},
{
"username": "username2",
"password": "password2"
}
]
}
credentials:
- username: username1
password: password1
- username: username2
password: password2
<Credentials>
<User Username="username1" Password="password1" />
<User Username="username2" Password="password2" />
</Credentials>
YAML has a simple format that's relatively easy for humans to read and write, without a lot of "fluff". It's capable of handling both simple and complex objects. It's also a superset of the JSON spec, so the same parser can read JSON formatting. I'd personally choose that. The YAMLDotNet library makes parsing pretty simple:
var dict = new DeserializerBuilder().Build().Deserialize<Dictionary<string, string>>(text);
Your use case sounds like exactly what ASP.NET's App Secrets (aka User Secrets) were designed to handle. Consider using their library, tooling, and conventions instead of coming up with your own.