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资源文件的操作的更多相关文章
- .net RESX资源文件
RESX资源文件最大的优势就是: 支持多语言 快速创建资源 管理方便 RESX可以支持多语言,Visual Studio编译后会出现附属程序集(satellite assembly),事实上是连接器( ...
- 解决asp.net mvc中*.resx资源文件访问报错
个人笔记 问题重现 在asp.net mvc中,使用资源文件会出现一个问题,例如: 紧接着我进入视图界面,输入下面代码: <a href="javascript:void(0);&qu ...
- 解决 IDEA 中src下xml等资源文件无法读取的问题
该问题的实质是,idea对classpath的规定. 在eclipse中,把资源文件放在src文件夹下,是可以找到的: 但是在idea中,直接把资源文件放在src文件夹下,如果不进行设置,是不能被找到 ...
- C#调用Resources.resx资源文件中的资源
使用到了.NET中的资源文件,也就是Resources.resx,于是就学会了如何调用资源文件中的资源.首先,资源文件可以从项目属性中的资源标签添加.比如,我添加一个图片,叫做aaa.png,添加入资 ...
- Linux下快速迁移海量文件的操作记录
有这么一种迁移海量文件的运维场景:由于现有网站服务器配置不够,需要做网站迁移(就是迁移到另一台高配置服务器上跑着),站点目录下有海量的小文件,大概100G左右,图片文件居多.目测直接拷贝过去的话,要好 ...
- NPOI在无Office环境下,对Office文件的操作
在做项目的时候,经常会遇到对 Office的操作,但有时候会没有Office环境,因此给大家介绍一个思路,在没有Office环境下,对Office的处理. NPOI,顾名思义,就是POI的.NET版本 ...
- 关于C#资源文件操作的总结
// 在这里,我来总结一下关于资源文件的相关操作. //1. 比较常见的有获取资源文件对应的文件流,然后转换到相对应的文件 // 比较典型的做法是通过代码程序集加载指定资源 // 如下通过Assemb ...
- 关于C#资源文件的相关操作
关于资源文件的相关操作. //1.比较常见的有获取资源文件对应的文件流,然后转换到相对应的文件 //比较典型的做法是通过代码程序集加载指定资源 //如下通过Assembly的静态方法GetExecut ...
- 转:C# 使用资源文件 Resource.resx 的方法
在写程序时, 可以把用到的 图标,图片,声音等外部资源,放在一个 .resx (资源文件)中. 这样的好处是不用考虑什么路径的问题.而且还对资源有保护的做用. 1.创建一个 ResourceFile ...
随机推荐
- 从0到1打造直播 App(直播流程介绍整理 <mark><转>)
注明:原创 2016-10-27 李智文 腾讯Bugly 概要 分享内容: 互联网内容载体变迁历程,文字——图片/声音——视频——VR/AR——……..从直播1.0秀场时代(YY),2.0游戏直播(斗 ...
- js 进阶 10 js选择器大全
js 进阶 10 js选择器大全 一.总结 一句话总结:和css选择器很像 二.JQuery选择器 原生javaScript中,只能使用getELementById().getElementByNam ...
- LUOGU 1137 - 拓扑排序
传送门 题目分析 拓扑排序:将图从度为0的点不断的剥掉外层的点,即可得到拓扑序,再按照拓扑序进行一遍简单的dp. code #include<bits/stdc++.h> using na ...
- js取json对象的键和值
//构建一个json对象 var pinpai = { "0":{"美的":49,"三星":35,"海信":25,&qu ...
- 【t045】细菌
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 近期,农场出现了D (1<= D <=15)种细菌.John 要从他的 N (1<= ...
- 视频和音频播放的演示最简单的例子9:SDL2广播PCM
===================================================== 最简单的视频和音频播放的演示样品系列列表: 最简单的视音频播放演示样例1:总述 最简单的视音 ...
- MySQL执行计划分析
原文:MySQL执行计划分析 一. 执行计划能告诉我们什么? SQL如何使用索引 联接查询的执行顺序 查询扫描的数据函数 二. 执行计划中的内容 SQL执行计划的输出可能为多行,每一行代表对一个数据库 ...
- SecureCRT 向多个终端发送相同命令
选中view,选择command windows 在下方出现的窗口中右键,接下来在窗口中输入命令即可,可以一定程度上代替分发脚本,具体请参考https://www.cnblogs.com/tele-s ...
- Swift 中的Closures(闭包)详解
Swift 中的Closures(闭包)详解 在Swift没有发布之前,所有人使用OC语言编写Cocoa上的程序,而其中经常被人们讨论的其中之一 -- Block 一直备受大家的喜爱.在Swift中, ...
- 调用cordova相关插件进行消息推送(通知栏提醒、响铃、震动)
原文:调用cordova相关插件进行消息推送(通知栏提醒.响铃.震动) 版权声明:本文为博主原创文章,转载须注明出处,博客地址:https://blog.csdn.net/wx13227855087 ...