【问题标题】:dynamically add values to resources (C#)动态地向资源添加值 (C#)
【发布时间】:2013-04-14 19:30:41
【问题描述】:

我想通过编码(C#) 为资源动态添加值。我下面的编码运行没有任何错误,但值没有被添加到资源文件中。

protected void Button2_Click(object sender, EventArgs e)
{
    using (ResXResourceWriter resx = new ResXResourceWriter("Resources.resx"))
    {
        resx.AddResource( "joth", "joth");
        resx.Close();
    }
}      

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 我认为这可能是您正在寻找的。我希望它对你有帮助! stackoverflow.com/questions/5686370/…
  • 尝试了一切,但什么都没有。应该在大括号(“Resources.resx”)中提供什么值是我使用的仪式?
  • 添加保存更改因为你添加成功但没有保存,所以在关闭之前保存更改。
  • 我如何编写代码来保存所做的更改?

标签: c# asp.net visual-studio-2010 azure resx


【解决方案1】:
protected void Button2_Click(object sender, EventArgs e)
{
    using (ResXResourceWriter resx = new ResXResourceWriter("Resources.resx"))
    {   resx.AddResource( "joth", "joth");
        resx.Save();
        resx.Close();
    }
} 

【讨论】:

  • 如果我使用 resx.save(); (错误 1 ​​'System.Resources.ResXResourceWriter' 不包含 'Save' 的定义,并且找不到接受类型为 'System.Resources.ResXResourceWriter' 的第一个参数的扩展方法 'Save'(您是否缺少 using 指令或程序集参考?)C:\Users\Jothieshwar\Desktop\jothieshwar\Backup\Project\begin\CS\RDImageGallery_WebRole\login.aspx.cs 113 22 RDImageGallery_WebRole)
【解决方案2】:

我尝试了上面的方法,它似乎不起作用,我环顾四周,尝试像 xml 文件一样编辑 resx 文件,它对我有用。

<data name="v13" xml:space="preserve">
   <value>Test TEst</value>
</data>

以上是nodepadd++中打开的resx文件中单个键/值对的结构

XDocument doc = XDocument.Load(Server.MapPath(@"~\App_GlobalResources\myResource2.resx"));

XElement data = new XElement("data");

XNamespace ns = "xml";
data.Add(new XAttribute("name", "v13"));
data.Add(new XAttribute(XNamespace.Xml + "space", "preserve"));

data.Add(new XElement("value", "Test TEst"));

doc.Element("root").Add(data);
doc.Save(Server.MapPath(@"~\App_GlobalResources\myResource2.resx"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2021-01-17
    • 2020-02-11
    • 1970-01-01
    相关资源
    最近更新 更多