加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

写入resx文件c#

发布时间:2020-12-15 23:32:59 所属栏目:百科 来源:网络整理
导读:试图写入用xml编写的Resx文件. 我将如何添加列和行. Liststring _paths = new Liststring { ConfigurationManager.AppSettings["SpanPath"],ConfigurationManager.AppSettings["FrenPath"],ConfigurationManager.AppSettings["RusPath"] }; ResourceWriter r
试图写入用xml编写的Resx文件.

我将如何添加列和行.

List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"],ConfigurationManager.AppSettings["FrenPath"],ConfigurationManager.AppSettings["RusPath"] };

        ResourceWriter resourceWriter = new ResourceWriter(_paths.ElementAt(0));

        resourceWriter.AddResource("Key1","String1");
        resourceWriter.AddResource("Key2","String2");
        resourceWriter.Close();

我想添加key1并在该行旁边的列中包含string1,依此类推.

我想我不明白msdn如何解释应该使用资源编写器的方式.

解决方法

您缺少resourceWriter.Generate()调用.

List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"],ConfigurationManager.AppSettings["RusPath"] };

using(ResXResourceWriter resourceWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
    resourceWriter.AddResource("Key1","String1");
    resourceWriter.AddResource("Key2","String2");
    resourceWriter.Generate();
}

编辑.如果丢失旧密钥,可以将它们存储在哈希表中,将新密钥添加到哈希表中,并从哈希表中重新生成resx.

using System.Resources;

List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"],ConfigurationManager.AppSettings["RusPath"] };

Hashtable oHt = new Hashtable();

// Read the keys and store in a hash table
using (ResXResourceReader oReader = new ResXResourceReader(_paths.ElementAt(0)))
{
     IDictionaryEnumerator oResource = oReader.GetEnumerator();
     while (oResource.MoveNext())
             oHt.Add(oResource.Key,oResource.Value);
}

//Add the new keys to the hash table
oHt["Key1"] = "String1";
oHt["Key2"] = "String2";

//Re-generate the new  resx from the hash table
using (ResXResourceWriter oWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
      foreach (string key in oHt.Keys)
           oWriter.AddResource(key.ToString(),oHt[key].ToString());
      oWriter.Generate();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读