Reputation:
I have many prompts to be used in the code, each prompt is a string such as "welcome to our company" or "goodby" etc.
Now I want to manage these prompts. There are two ways, one is to store each string in a file then load it in the code.
private string LoadPrompt(string inid, string PromptsPath,string promptFile)
{
StringBuilder filetopen = new StringBuilder();
StringBuilder content = new StringBuilder();
filetopen.Append(PromptsPath + inid + "_" + promptFile);
try
{
if (File.Exists(filetopen.ToString()))
{
using (StreamReader reader = File.OpenText(filetopen.ToString()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
content.Append(line);
}
}
}
}
catch (Exception ex)
{
AppLogEx(ex, "Utilities:LoadPrompt");
content.Clear();
content.Append("ERROR");
}
return content.ToString();
}
The other one is put prompts in app.config. Which way is better and fast to load them?
Upvotes: 1
Views: 111
Reputation: 415600
Set up the strings as Resources.
The advantage to using resources over a flat file or app.config is that you can then localize your strings into various languages, and there is support and tooling available to make this process much easier. If you provide multiple languages, your program can automatically select the appropriate language/culture based on the local system without you needing to do any extra work.
Upvotes: 4
Reputation: 4778
About the code, you can do this simpler
instead
using (StreamReader reader = File.OpenText(filetopen.ToString()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
content.Append(line);
}
}
you can write
File.ReadAllLines(filetopen.ToString())
.ToList() - added only for Foreach, I think you've your own Foreach extension for IEnumerable
.ForEach(x => content.AppendLine(x));
about prompts, config or resources
Upvotes: 1
Reputation: 10152
Well, if you do it through app.config, it's essentially an XML file. So you'll be able to find the right prompt fairly easily. Since it's a setting file, it would make sense to store in it any settings that you might change down the road. You can do the same thing with a regular text file, but then you'll need to somehow mark different prompts... in which case, might as well use XML.
Upvotes: 0