原文: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. .net RESX资源文件

    RESX资源文件最大的优势就是: 支持多语言 快速创建资源 管理方便 RESX可以支持多语言,Visual Studio编译后会出现附属程序集(satellite assembly),事实上是连接器( ...

  2. 解决asp.net mvc中*.resx资源文件访问报错

    个人笔记 问题重现 在asp.net mvc中,使用资源文件会出现一个问题,例如: 紧接着我进入视图界面,输入下面代码: <a href="javascript:void(0);&qu ...

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

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

  4. C#调用Resources.resx资源文件中的资源

    使用到了.NET中的资源文件,也就是Resources.resx,于是就学会了如何调用资源文件中的资源.首先,资源文件可以从项目属性中的资源标签添加.比如,我添加一个图片,叫做aaa.png,添加入资 ...

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

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

  6. NPOI在无Office环境下,对Office文件的操作

    在做项目的时候,经常会遇到对 Office的操作,但有时候会没有Office环境,因此给大家介绍一个思路,在没有Office环境下,对Office的处理. NPOI,顾名思义,就是POI的.NET版本 ...

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

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

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

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

  9. 转:C# 使用资源文件 Resource.resx 的方法

    在写程序时, 可以把用到的 图标,图片,声音等外部资源,放在一个  .resx (资源文件)中. 这样的好处是不用考虑什么路径的问题.而且还对资源有保护的做用. 1.创建一个 ResourceFile ...

随机推荐

  1. 使用搜狐Sendcloud的Webapi发送邮件:Jodd和Apache Httpclient

    最近,在使用搜狐Sendcloud发邮件.    Sendcloud提供http格式的webapi,方便地发送邮件,当然是要付费的. 很早之前,http工具一直用Httpclient,后来觉得jodd ...

  2. 【a502】符号三角形问题

    Time Limit: 1 second Memory Limit: 32 MB [问题描述] 在一般情况下,符号三角形的第一行有n个符号.按照2个同号的下面是"+"号,2个异号的 ...

  3. [NativeScript] Create new application and run emulator

    Install: npm i -g nativescript Create: tns create <app_name> --ng Run: tns emulate ios List al ...

  4. php课程 6-24 字符串函数有哪些(复习)

    php课程 6-24 字符串函数有哪些(复习) 一.总结 一句话总结: 二.php课程 6-24 字符串函数有哪些(复习) 上次复习:--------------------------------- ...

  5. Hierarchical Tree Traversal in Graphics Pipeline Stages

    BACKGROUND Many algorithms on a graphics processing unit (GPU) may benefit from doing a query in a h ...

  6. [React Native] Installing and Linking Modules with Native Code in React Native

    Learn to install JavaScript modules that include native code. Some React Native modules include nati ...

  7. Xcode编译Undefined symbols for architecture xxx 错误总结

    可能会遇到这几种错误:Undefined symbols for architecture armv7Undefined symbols for architecture armv7sUndefine ...

  8. jQuery实现复选框的全选、反选、并且根据复选框的<checked属性>控制多个对应div的显示/隐藏

    <!doctype html><html> <head> <meta charset="utf-8"> <title>j ...

  9. 【序列操作V】平衡树(无旋treap)

    题目描述 维护一个队列,初始为空.依次加入 n(1≤n≤105)个数 ai(-109≤ai≤109),第 i(1≤i≤n)个数加入到当前序列第 bi(0≤bi≤当前序列长度)个数后面.输出最终队列. ...

  10. Spring Boot 定制与优化内置的Tomcat容器

    1.Spring Boot定制与优化内置Tomcat容器. > 内置的容器有三个分别是Undertow.Jetty.Tomcat,Spring Boot 对这三个容器分别进行了实现,它们上层接口 ...