C# 如何使用程序添加查询和编辑resx文件(资源文件)

在C#项目中难免用到资源文件(以resx结尾的文件)

读取和添加的方法:

在项目中首先要引用命名空间

using System.Resources;

string respath=@".Resources1.resx";

//给资源文件中写入字符串

using(ResXResourceWriter resx=new ResXResourceWriter(respath))

{

    resx.AddResource("String1","你想要存储的字符串");

}

//读取资源文件的值

using(ResXResourceSet rest=new ResXResourceSet(respath))

{

    string str=rest.GetString("String1");

}

//以下是编辑资源文件的值,资源文件是以XML文件格式存储的,我们就一XML文件方式编辑它

XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(respath);
            XmlNodeList xnlist = xmlDoc.GetElementsByTagName("data");//这个data是固定
            foreach (XmlNode node in xnlist)
            {
                if (node.Attributes != null)
                {
                    if (node.Attributes["xml:space"].Value == "preserve")//这个preserve也是固定的
                    {
                        if(node.Attributes["name"].Value=="String1")//String1是你想要编辑的
                        {
                            node.InnerText = "我成功了";//给他赋值就OK了
                        }
                    }
                }
            }
            xmlDoc.Save(respath);//别忘记保存

大功搞成!呵呵开玩笑的这个实际上也不难只要知道他存储的格式就可以去编辑它

原文地址:https://www.cnblogs.com/ImNo1/p/4619172.html