c# - 存储一个短字符串

标签 c# .net

我在代码中使用了很多提示符,每个提示符都是一个字符串,例如“欢迎来到我们公司”或“再见”等。

现在我想管理这些提示。有两种方法,一种是将每个字符串存储在一个文件中,然后在代码中加载它。

 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/

相关文章:

c# - 返回 IAsyncCursor 的 MongoDB C# 驱动程序 Mock 方法

c# - 从 DbDataReader 读取数据最快的方法是什么?

c# - 从sql express中的给定表中查找最大值

c# - 从 SQL Server 数据库获取数据集

.net - JSON解码: Unexpected token: StartArray

.net - 使用 .NET 3.5 安全地监听端口

c# - .NET 程序员的 JavaScript 编辑器

c# - 为什么要调用这个虚方法呢?

C# Math.Acos 如何防止 NaN

.net - 何时重写 OnEventMethod 与处理实际事件