VS下对Resx资源文件的操作

简介: 原文:VS下对Resx资源文件的操作 读取   using System.IO; using System.Resources; using System.Collections; using System.
原文: VS下对Resx资源文件的操作

读取

 

using System.IO; using System.Resources; using System.Collections; using System.Reflection; namespace ResxEdit { class ResxDemo { void ReadResx(string strResxPath, Boolean isResourcePath) { AssemblyName[] referencedAssemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); ResXResourceReader rsxResource = new ResXResourceReader(strResxPath); rsxResource.UseResXDataNodes = true; IDictionaryEnumerator enumerator = rsxResource.GetEnumerator(); while (enumerator.MoveNext()) { DictionaryEntry current = (DictionaryEntry)enumerator.Current; ResXDataNode node = (ResXDataNode)current.Value; string strKey = node.Name; //资源项名 string strValue = node.GetValue(referencedAssemblies); //值 string strComment = node.Comment; //注释 } } } }

 

写入

 

using System.IO; using System.Resources; using System.Collections; using System.Reflection; namespace ResxEdit { class ResxDemo { void WriteResx(string strSavePath) { ResXResourceWriter resourceWriter = new ResXResourceWriter(strSavePath); string strKey="Key"; //资源项名 string strValue="Value"; //值 string strComment="Comment"; //注释 ResXDataNode node = new ResXDataNode(strKey, strValue); node.Comment = strComment; resourceWriter.AddResource(node); resourceWriter.Generate(); resourceWriter.Close(); } } } 

 

以上是用于储存string类型的资源

 

对于其他资源类型 例如 图片音乐等

 

using System.Resources; using System.Collections; namespace ResxDemo { class ResxDemo { void WriteResx() { ResourceWriter rw = new ResourceWriter("./res.resx"); Bitmap map = new Bitmap("./123.jpg"); rw.AddResource("pp", map); rw.Generate(); rw.Close(); } void ReadResx() { ResourceReader rr = new ResourceReader("./res.resx"); foreach (DictionaryEntry dic in rr) { if (dic.Key.ToString() == "pp") { Bitmap map = new Bitmap((Bitmap)dic.Value); pictureBox1.Image = map; } } } } }  

 

 

当然也可以用ResourceManager来处理  

using System; using System.Resources; using System.Reflection; using System.Threading; using System.Globalization; /* Perform the following steps to use this code example: Main assembly: 1) In a main directory, create a file named "rmc.txt" that contains the following resource strings: day=Friday year=2006 holiday="Cinco de Mayo" 2) Use the resgen.exe tool to generate the "rmc.resources" resource file from the "rmc.txt" input file. > resgen rmc.txt Satellite Assembly: 3) Create a subdirectory of the main directory and name the subdirectory "es-ES", which is the culture name of the satellite assembly. 4) Create a file named "rmc.es-ES.txt" that contains the following resource strings: day=Viernes year=2006 holiday="Cinco de Mayo" 5) Use the resgen.exe tool to generate the "rmc.es-ES.resources" resource file from the "rmc.es-ES.txt" input file. > resgen rmc.es-ES.txt 6) Use the al.exe tool to create a satellite assembly. If the base name of the application is "rmc", the satellite assembly name must be "rmc.resources.dll". Also, specify the culture, which is es-ES. > al /embed:rmc.es-ES.resources /c:es-ES /out:rmc.resources.dll 7) Assume the filename for this code example is "rmc.cs". Compile rmc.cs and embed the main assembly resource file, rmc.resources, in the executable assembly, rmc.exe: >csc /res:rmc.resources rmc.cs 8) Execute rmc.exe, which obtains and displays the embedded resource strings. */ class Sample { public static void Main() { string day; string year; string holiday; string celebrate = "{0} will occur on {1} in {2}./n"; // Create a resource manager. The GetExecutingAssembly() method // gets rmc.exe as an Assembly object. ResourceManager rm = new ResourceManager("rmc", Assembly.GetExecutingAssembly()); // Obtain resources using the current UI culture. Console.WriteLine("Obtain resources using the current UI culture."); // Get the resource strings for the day, year, and holiday // using the current UI culture. Use those strings to // display a message. day = rm.GetString("day"); year = rm.GetString("year"); holiday = rm.GetString("holiday"); Console.WriteLine(celebrate, holiday, day, year); // Obtain the es-ES culture. CultureInfo ci = new CultureInfo("es-ES"); // Get the resource strings for the day, year, and holiday // using the specified culture. Use those strings to // display a message. // Obtain resources using the es-ES culture. Console.WriteLine("Obtain resources using the es-ES culture."); day = rm.GetString("day", ci); year = rm.GetString("year", ci); holiday = rm.GetString("holiday", ci); // --------------------------------------------------------------- // Alternatively, comment the preceding 3 code statements and // uncomment the following 4 code statements: // ---------------------------------------------------------------- // Set the current UI culture to "es-ES" (Spanish-Spain). // Thread.CurrentThread.CurrentUICulture = ci; // Get the resource strings for the day, year, and holiday // using the current UI culture. Use those strings to // display a message. // day = rm.GetString("day"); // year = rm.GetString("year"); // holiday = rm.GetString("holiday"); // --------------------------------------------------------------- // Regardless of the alternative that you choose, display a message // using the retrieved resource strings. Console.WriteLine(celebrate, holiday, day, year); } } /* This code example produces the following results: >rmc Obtain resources using the current UI culture. "5th of May" will occur on Friday in 2006. Obtain resources using the es-ES culture. "Cinco de Mayo" will occur on Viernes in 2006. */ 

 

目录
相关文章
|
Docker Windows 容器
cpu不支持avx指令集怎么办
如果CPU不支持AVX指令集,可以考虑以下两种解决方案: 更新BIOS版本:在某些情况下,更新BIOS版本可能会支持AVX指令集。可以联系电脑厂商或者查阅相关教程进行BIOS更新。 更换支持AVX指令集的CPU:如果更新BIOS版本后仍不支持AVX指令集,那么可以考虑更换支持AVX指令集的CPU。可以根据自己的需求和预算选择适合的CPU。 另外,如果在tf1.6以后的官方的tf包都是用AVX编译的,而电脑的CPU不支持AVX指令集,那么可以考虑使用Docker来配置运行环境。但需要注意,Docker在Windows上配置稍显繁琐,并需要配置虚拟机等其他东西。 总的来说,如果不支持AVX指令
8418 1
|
存储 开发工具 git
Vscode 拉取代码时出现 在签出前 请先清理仓库工作树
Vscode 拉取代码时出现 在签出前 请先清理仓库工作树
2511 0
【Groovy】Groovy 环境搭建 ( 下载 Groovy | 安装 Groovy | 配置 Groovy 环境变量 )
【Groovy】Groovy 环境搭建 ( 下载 Groovy | 安装 Groovy | 配置 Groovy 环境变量 )
933 0
【Groovy】Groovy 环境搭建 ( 下载 Groovy | 安装 Groovy | 配置 Groovy 环境变量 )
|
数据可视化 数据处理 数据库
【Python篇】PyQt5 超详细教程——由入门到精通(中篇一)
【Python篇】PyQt5 超详细教程——由入门到精通(中篇一)
1174 2
|
存储 JavaScript 网络安全
电脑上安装多个nodeJS版本实现一键切换
电脑上安装多个nodeJS版本实现一键切换
350 1
|
设计模式 前端开发 Java
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
文章详细介绍了中介者模式(Mediator Pattern),这是一种对象行为型模式,用于封装一系列对象的交互,降低系统耦合度,并简化对象之间的交互关系。通过案例分析、结构图、时序图和代码示例,文章展示了中介者模式的组成部分、实现方式和应用场景,并讨论了其优点、缺点和适用情况。
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
|
存储 测试技术 数据库
数据库备份的方法
数据库备份的方法
741 1
|
Java 数据库连接 mybatis
成功解决: Invalid bound statement (not found) 在已经使用mybatis的项目里引入mybatis-plus,结果不能共存的解决
这篇文章讨论了在已使用MyBatis的项目中引入MyBatis-Plus后出现的"Invalid bound statement (not found)"错误,并提供了解决方法,主要是通过修改yml配置文件来解决MyBatis和MyBatis-Plus共存时的冲突问题。
成功解决: Invalid bound statement (not found) 在已经使用mybatis的项目里引入mybatis-plus,结果不能共存的解决
|
设计模式 缓存 架构师
创建型设计模式的比较与决策
本文深入探讨六种创建型设计模式:单例模式、简单工厂模式、工厂方法模式、抽象工厂模式、建造者模式和原型模式。通过概述各模式的核心概念、应用场景和关键要素,帮助读者理解并掌握这些模式的核心思想。文章还通过比较表和决策流程图,直观地展示了各模式之间的差异和选择依据,为读者提供了实用的设计模式选择指南。 本文的特色在于结合了理论与实践,每个模式都配有详细的代码示例和结构图,帮助读者更好地理解和应用这些模式。 总之,本文旨在为读者提供一篇全面、深入且实用的创建型设计模式指南,帮助读者在实际工作中灵活运用这些模式...
401 0
创建型设计模式的比较与决策
|
数据可视化 JavaScript 前端开发
使用 ECharts 绘制3D饼图,立体效果华丽渲染!
使用 ECharts 绘制3D饼图,立体效果华丽渲染!