VS下对Resx资源文件的操作

简介: 原文:VS下对Resx资源文件的操作 读取   using System.IO; using System.Resources; using System.Collections; using System.
原文: 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. */ 

 

目录
相关文章
|
Java 开发工具 git
获取 /resources 目录资源文件的 9 种方法,还有谁不会?!-2
获取 /resources 目录资源文件的 9 种方法,还有谁不会?!
|
Java Spring
获取 /resources 目录资源文件的 9 种方法,还有谁不会?!-1
获取 /resources 目录资源文件的 9 种方法,还有谁不会?!
284 0
|
Java Maven
Maven项目获取资源文件路径并读取资源文件内容
Maven项目获取资源文件路径并读取资源文件内容
216 0
Maven项目获取资源文件路径并读取资源文件内容
C#获取资源文件夹中的完整路径
C#获取资源文件夹中的完整路径
|
C#
C# .Resx文件无效解决方案 Resx文件报错解决方法
C# .Resx文件无效解决方案 Resx文件报错解决方法
586 7
C# .Resx文件无效解决方案 Resx文件报错解决方法
从加载DLL的中获取放置于Resources文件夹中资源字典的几种方法
原文:从加载DLL的中获取放置于Resources文件夹中资源字典的几种方法 主程序 为 Main_Test.exe 被加载的DLL 为 Load_Test.dll  此DLL 中 有一个 文件夹Resources文件夹有一个资源字典Graphics.
1282 0
|
索引 图形学 测试技术
Resources文件夹解析
翻译自官网文档:https://unity3d.com/cn/learn/tutorials/topics/best-practices/resources-folder 2.1。
926 0