以编程方式创建资源文件

时间:2009-12-12 22:54:34

标签: c# resources

我想创建一个资源文件(在准备部署的过程中),用某些设置(大型XML结构)和一些文本填充它,但我不知道如何去做。

我确实使用ResXResourceWriter找到了一些示例,但在尝试打开它时无法找到资源键。这是我到目前为止所做的:

private void simpleButton1_Click(object sender, EventArgs e)
{
    using (System.IO.MemoryStream oStream = new System.IO.MemoryStream())
    {
        this.layoutControl1.SaveLayoutToStream(oStream);
        using (ResXResourceWriter oWriter = new ResXResourceWriter(@"..\..\Properties\LayoutControl.resources.Resx"))
        {
            oWriter.AddResource("one", oStream.GetBuffer());
            oWriter.Generate();
            oWriter.Close();
        }
    }

}

private void simpleButton2_Click(object sender, EventArgs e)
{
    ResourceManager rm = new ResourceManager("WindowsFormsApplication1.LayoutControl", Assembly.GetExecutingAssembly());
    var one = rm.GetObject("one");
    Console.WriteLine("");
}

我通过点击simpleButton1来创建资源,然后停止应用,在我的项目中添加一个add-existing-item,重新编译并点击simpleButton2,然后我得到一个

  

MissingManifestResourceException(无法找到适合指定文化或中性文化的任何资源。确保在编译时正确嵌入或链接到程序集“WindowsFormsApplication1”中的“WindowsFormsApplication1.LayoutControl.resources”,或者所有附属程序集都需要可加载并完全签名。)

有人可以给我一些指示或更好,一个有效的例子吗?如果资源可以编译成像'普通'资源文件那样的'单独'程序集,我宁愿使用它。

1 个答案:

答案 0 :(得分:8)

你非常接近 - 你传递给ResourceManager的baseName有点过了。

您编译的任何resx文件都使用默认命名空间 plus 命名路径中的任何文件夹。在你的帖子中,你应该传入“WindowsFormsApplication1.Properties.LayoutControl.resources”。

相关问题