以编程方式创建资源文件

Jur*_*jen 12 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("");
}
Run Code Online (Sandbox Code Playgroud)

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

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

一个人能给我一些指针还是更好,一个有效的例子?如果资源可以编译成像'普通'资源文件那样的'单独'程序集,我宁愿这样做.

小智 8

你非常接近 - 你传递给ResourceManager的baseName有点偏.

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