【问题标题】:c# How do I write to a .resx file? [duplicate]c# 如何写入 .resx 文件? [复制]
【发布时间】:2016-03-17 04:14:57
【问题描述】:

我找到了这个如何从 .resx 文件中读取的示例

    public static void ReadResourceFile()
    {
        ResourceManager rm = new ResourceManager("XmlReadAndWritePractice.Resource1", Assembly.GetExecutingAssembly());
        //String strWebsite = rm.GetString("Website",CultureInfo.CurrentCulture);   
        String strName = rm.GetString("FirstName");
        Console.WriteLine(strName);

    }

但是如何编辑或添加到 .resx 文件?我找到了以下示例,但这不是我所期望的。

    private static void CreateResourceFile()
    {
        // Creates a resource writer.
        IResourceWriter writer = new ResourceWriter("c:\\temp\\Resource1.resx");

        // Adds resources to the resource writer.
        writer.AddResource("String 1", "First String");

        writer.AddResource("String 2", "Second String");

        writer.AddResource("String 3", "Third String");

        // Writes the resources to the file or stream, and closes it.
        writer.Close();

    }

资源参考

ResourceManager

ResourceWriter

【问题讨论】:

标签: c#


【解决方案1】:

这是因为您正在生成 resx 文件的二进制文件。

你试过了吗?

https://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx

【讨论】:

    【解决方案2】:

    您需要在关闭前调用 generate。

    writer.Generate();
    writer.Close();
    

    【讨论】:

    • Close 已经节省了资源 - 所以不需要打电话给Generate,但更重要的是,这个建议甚至没有试图回答 OP 在查看时获取资源的二进制版本(.resource 格式)的问题用于文本输出(.resx)。
    【解决方案3】:
    private static void CreateResourceFile()
    {
        using (ResXResourceWriter writer = new ResXResourceWriter("c:\\temp\\Resource1.resx"))
        {
            writer.AddResource("String 1", "First String");
            writer.AddResource("String 2", "Second String");
            writer.AddResource("String 3", "Third String");
            writer.Generate();
        }
    }
    

    【讨论】:

    • 所以显然我的问题是我在控制台应用程序中执行此操作。一旦我尝试使用 Windows Forms 应用程序,它就可以完美运行。有什么见解吗?
    • 这可能与 ResXResourceWriter 作为 winforms 程序集的一部分有关。您可以使用ResXResourceReader.NetStandard 将它们单独打包
    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2013-02-08
    • 2013-10-02
    • 2013-06-14
    相关资源
    最近更新 更多