我在代码中使用了很多提示符,每个提示符都是一个字符串,例如“欢迎来到我们公司”或“再见”等。
现在我想管理这些提示。有两种方法,一种是将每个字符串存储在一个文件中,然后在代码中加载它。
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();
}
另一个是在 app.config 中放置提示。哪种加载方式更好更快?
最佳答案
设置字符串为Resources .
在平面文件或 app.config 上使用资源的优势在于,您可以将字符串本地化成各种语言,并且有可用的支持和工具来简化此过程。如果您提供多种语言,您的程序可以根据本地系统自动选择合适的语言/文化,而无需您做任何额外的工作。
关于c# - 存储一个短字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22820800/