如何更新.resx文件中的值?

时间:2018-10-12 09:28:56

标签: c# asp.net-mvc-4 resources

在我的控制器中,我从要更改.resx文件的视图中获取所有值,但是它不起作用-这些值在.resx文件中保持不变。

    [HttpPost]
    public ActionResult CustimizeSample(FormCollection fc)
    {
        string formname = Request.Form["formname"];
        var lablename = Request.Form["lablename"];
        var hintname = Request.Form["hintname"];
        var buttonname = Request.Form["buttonname"];
        Resources.Resource1.InsertSample_Form_Name.Replace(@Resources.Resource1.InsertSample_Form_Name, formname);
        Resources.Resource1.InsertSample_lable_Sample_Name.Replace(@Resources.Resource1.InsertSample_lable_Sample_Name, lablename);
        Resources.Resource1.InsertSample_Hint_Name.Replace(@Resources.Resource1.InsertSample_Hint_Name, hintname);
        Resources.Resource1.InsertSample_Button_Name.Replace(@Resources.Resource1.InsertSample_Button_Name, buttonname);

        return View();
    }

这是我的视图,在其中,我从用户那里获取.resx文件中更改值的输入。enter image description here

1 个答案:

答案 0 :(得分:1)

 var reader = new ResXResourceReader(@"C:\CarResources.resx");//same fileName
 var node = reader.GetEnumerator();
 var writer = new ResXResourceWriter(@"C:\CarResources.resx");//same fileName(not new)
 while (node.MoveNext())
         {
     writer.AddResource(node.Key.ToString(), node.Value.ToString());
       }
  var newNode = new ResXDataNode("Title", "Classic American Cars");
  writer.AddResource(newNode);
  writer.Generate();
  writer.Close();