VS下对Resx资源文件的操作

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

时间: 2024-11-05 18:42:33

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

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

个人笔记 问题重现 在asp.net mvc中,使用资源文件会出现一个问题,例如: 紧接着我进入视图界面,输入下面代码: 1 <a href="javascript:void(0);">测试@KuaiLeYouNi.Web.AppResource.Space</a> 以上编译不会报错,但是运行是会报错:“编译器错误消息: CS0122: “KuaiLeYouNi.Web.AppResource”不可访问,因为它受保护级别限制” 问题解决 原来资源文件默认的访问修

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

该问题的实质是,idea对classpath的规定. 在eclipse中,把资源文件放在src文件夹下,是可以找到的: 但是在idea中,直接把资源文件放在src文件夹下,如果不进行设置,是不能被找到的. 下面说说几种解决方法,网上说的都很混乱,我这里做一个总结:推荐方法4 1.将所有资源文件放在resources文件夹下 这样做很方便,比较容易想到,但是层次性就很差了,比如mybatis的映射配置文件mapper.xml,本来需要放在特定的包里面,与dao层,service层等层次为同一个层级

Servlet-config.properteis资源文件读取操作

java文件 import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.h

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

有这么一种迁移海量文件的运维场景:比如由于现有网站服务器配置不够,需要做网站迁移(就是迁移到另一台高配置服务器上陪着),站点目录下有海量的小文件,大概100G左右,图片文件居多.目测直接拷贝过去的话,要好几天的时间.那么问题来了,这种情况下的网站数据要怎么迁移呢?另外,此网站还在运行中,白天是断然不能停止了,只能运行深夜停掉几个小时. 可以采用的方案如下:1.利用rsync进行同步.这种方法速度会慢,不过好在支持续传,在带宽不高或网站不稳定的情况下强烈建议用此方法:1)先修改一下旧站上传图片的功

linux下删除特殊字符命名文件的操作记录

在linux下,有时候会碰到以特殊字符命名的文件,要删除这些文件需要加转义符号.下面列出几个例子看看吧: [[email protected] tmp]# touch \(22\)[[email protected] tmp]# touch \1231[[email protected] tmp]# touch \\1231[[email protected] tmp]# touch \<22:23\>[[email protected] tmp]# ll-rw-r--r-- 1 root r

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

原文地址:https://www.cnblogs.com/it-alibaba/p/9141670.html

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

// 在这里,我来总结一下关于资源文件的相关操作. //1. 比较常见的有获取资源文件对应的文件流,然后转换到相对应的文件 // 比较典型的做法是通过代码程序集加载指定资源 // 如下通过Assembly 的静态方法GetExecutingAssembly() 得到程序集 // 还有很多方式可以得到代码程序集 C#代码                           System.Reflection.Assembly asm = System.Reflection.Assembly.Get

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

关于资源文件的相关操作. //1.比较常见的有获取资源文件对应的文件流,然后转换到相对应的文件 //比较典型的做法是通过代码程序集加载指定资源 //如下通过Assembly的静态方法GetExecutingAssembly()得到程序集 //还有很多方式可以得到代码程序集 System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); Stream manifestResourceStream

读取web应用下的资源文件(例如properties)

1 package gz.itcast.b_resource; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Properties; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRe