原文: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. */

VS下对Resx资源文件的操作相关推荐

  1. python3读取网页_python3+selenium获取页面加载的所有静态资源文件链接操作

    软件版本: python 3.7.2 selenium 3.141.0 pycharm 2018.3.5 具体实现流程如下,废话不多说,直接上代码: from selenium import webd ...

  2. idea开发MAVEN项目target 下 mapper.xml资源 文件

    异常信息 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yaosiyuan. ...

  3. 解决 IDEA 中src下xml等资源文件无法读取的问题

    该问题的实质是,idea对classpath的规定. 在eclipse中,把资源文件放在src文件夹下,是可以找到的: 但是在idea中,直接把资源文件放在src文件夹下,如果不进行设置,是不能被找到 ...

  4. android 图片特征提取比对_判断ImageView当前的图片是哪个图片,然后比对资源文件的操作...

    描述一下需求:我们在点击一个ImageView的时候进行,切换背景,或者是资源文件的时候,我们需要根据拿到的背景进行比对,(试了好几种方法,总是出现一些小毛病) 例如:网上的某某人写的这种: if ( ...

  5. Linux下快速迁移海量文件的操作记录

    有这么一种迁移海量文件的运维场景:由于现有网站服务器配置不够,需要做网站迁移(就是迁移到另一台高配置服务器上跑着),站点目录下有海量的小文件,大概100G左右,图片文件居多.目测直接拷贝过去的话,要好 ...

  6. linux 误删除mysql表能恢复吗_Linux下Oracle误删除数据文件恢复操作

    检查数据文件的位置如下: SQLgt; select name from v$datafile; NAME----------------------------------------------- ...

  7. linux oracle删除恢复数据恢复,Linux下Oracle误删除数据文件恢复操作

    检查数据文件的位置如下: SQL> select name from v$datafile; NAME --------------------------------------------- ...

  8. 关于C#资源文件操作的总结

    //在这里,我来总结一下关于资源文件的相关操作. //1.比较常见的有获取资源文件对应的文件流,然后转换到相对应的文件 //比较典型的做法是通过代码程序集加载指定资源 //如下通过Assembly的静 ...

  9. IDEA maven项目src源代码下的资源文件不自动复制到classes文件夹的解决方法

    在学习spring boot的时候,书中的代码是把java文件和资源文件放到了同一个文件夹下,在idea maven项目下面运行会一直报错,因为资源文件不会自动复制到classes文件夹下,如下图: ...

最新文章

  1. Asp.net之MsChart控件动态绑定温度曲线图
  2. log4j 源码解析_log4j2源码解析(2)--LoggerContext
  3. day21—AngularJS学习初体验
  4. 将Sublime Text 添加到鼠标右键菜单的教程方法
  5. Portlet MVC框架
  6. Android-- SharedPreferences数据存储XML文件的方法
  7. mysql数据库中实现内连接、左连接、右连接
  8. HTML5 的输入类型(input type)
  9. Wunderlist 云端任务管理(Todo list)工具
  10. 毕业论文答辩技巧及注意事项(附答辩自述模板)~
  11. Python去除文章首尾空格换行符
  12. java基础_设计模式_设计基础(小鸭子游戏)
  13. java手机震动_Android实现手机震动抖动效果的方法
  14. 用户画像-User Profile
  15. 第四次的面试 C++ 面试 (迷茫) + (复习知识点)
  16. python毕业设计作品基于django框架的鲜花水果商城毕设成品(7)中期检查报告
  17. cdh报 Unable to acquire JDBC Connection
  18. 【数据结构】第六章:图(上)
  19. 男生想跟女生说的30句真心话!!!!
  20. 为什么“灵活用工”将成为影响企业寿命的关键?

热门文章

  1. linux内核模块的优缺点
  2. 俄罗斯显卡测试软件,《战地1》正式版显卡测试:EA教众厂商什么叫优化
  3. c++ opencv 照片清晰度_opencv新手学习————素描视频(C++)
  4. jenkins 手动执行_Jenkins(一)
  5. java控制器删除数据_javaWeb删除一条及多条数据
  6. modelsim-win64-10.4-se 破解(win7实验成功)(其他操作系统也可参考,大同小异)
  7. 不要随意重装你的操作系统
  8. [Angular Tutorial] 11 -Custom Filters
  9. mysql之DML(SELECT DELETE INSERT UPDATE)
  10. 老话题,不要在遍历容器中增删容器数据