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

最新文章

  1. vue.js组件化开发实践
  2. js中对象 类 实例的区别 数据类型 创建对象
  3. win下Redis安装使用
  4. VB.NET TextBox 只允许输入1-100之间的整数 简洁篇
  5. extjs简单动画2
  6. CentOS6.5菜鸟之旅:VirtualBox4.3识别USB设备
  7. rem详解及使用方法
  8. java面试题中常见的关于String类问题总结
  9. MyEclipse8.5集成Tomcat7
  10. [Java] Eclipse注释模板设置详解
  11. 将ANGULAR与后端请求结合
  12. 简单dp-poj-2231-Moo Volume
  13. php中使用伪静态
  14. 在ASP.NET MVC中使用 Bootstrap table插件
  15. MySQL存储写入性能严重抖动分析
  16. 【原创】2、小程序域名配置之申请支持SSL(https)
  17. 吴恩达深度学习第1课第3周编程作业记录(2分类1隐层nn)
  18. vue 页面跳转的两种方式
  19. jquery 中dataTable显示加载中,图片或文字
  20. 【Tomcat】Tomcat的类加载机制

热门文章

  1. [React] Recompose: Override Styles & Elements Types in React
  2. kindeditor4跨域上传图片解决
  3. SpringMVC大坑一枚:ContentNegotiatingViewResolver可能不利于SEO
  4. wpf datagrid 的单元格内容超出列宽度
  5. win32命令行小程序获取指定文件夹或者目录下面的所有文件大小,文件数量,目录数量
  6. VS2017十五项新功能体验
  7. How to provide highlighting with Spring data elasticsearch
  8. sdk manager 打不开
  9. APP和服务端-架构设计(二)
  10. 在Winform或WPF中System.Diagnostics.Process.Start的妙用