您的位置:首页 > 其它

VS下对Resx资源文件的操作

2016-05-16 11:52 429 查看
原文: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.
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: