c#如何写入.resx文件?

时间:2016-03-17 04:14:57

标签: c#

我找到了如何从.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();

    }

enter image description here

资源参考

ResourceManager

ResourceWriter

3 个答案:

答案 0 :(得分:1)

您需要在收盘前调用生成。

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

答案 1 :(得分:1)

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

你试过这个吗?

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

答案 2 :(得分:1)

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();
    }
}