从Setting.settings到Resource.resx
生活随笔
收集整理的這篇文章主要介紹了
从Setting.settings到Resource.resx
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前由于經驗不足,將常用的App提示信息串(string)放置在了配置文件中(*.Settings)。目前需要將App國際化,對這些信息的翻譯有兩個途徑: 直接翻譯,將參數中的提示信息串用英文或者其他語言替換。這在語義上是改變了App的配置參數。 將這些提示信息串提取出來,放入資源文件,統一對資源文件進行國際化。 個人更傾向于第2個方案。一是因為語義上比較一致;二是可以將資源統一處理。更可以建立新的DLL項目,將資源放置在DLL中。采用第2個方案需要兩個
步驟:1.將Settings中的名值對提取出來;2.將名值對生成為資源文件。如下Code,提取出名值對,以key=value的形式保存在中間文件(.txt)中。該中間文件
能夠被Resgen.exe進行識別,利用resgen.exe生成C#強類型的資源文件。
*資源文件生成器 (Resgen.exe) 將文本(.txt 或 .restext)文件和基于 XML 的資源格式 (.resx) 文件轉換為公共語言運行時二進制 (.resources) 文件,后者可嵌入到運行時二進制可執行文件或附屬程序集中。
示例Code如下:
/// <summary>/// 將制定的參數集合以名值對的形式轉儲到制定的文本文件(utf-8)中。/// </summary>/// <param name="stettings">參數集合</param>/// <param name="txtPath">輸出路徑</param>/// <exception cref="System.IO.IOException"></exception>public static void DumpSettingsToKvTxt(SettingsPropertyCollection stettings, string txtPath){using (var fio = new StreamWriter(txtPath, true, Encoding.UTF8, 2048)){ foreach (var p in stettings){var sp = p as SettingsProperty;fio.WriteLine($"{sp.Name}={sp.DefaultValue.ToString()}");}fio.Flush();}}/// <summary>/// resgen Hello.resource.txt /str:cs Hello.resource.resx/// </summary>/// <param name="txtPath">名值對的txt文本</param>/// <param name="resxName">目標強類型(C#/cs)*.resx資源文件</param>public static void ConvertKvTxtToResx(string txtPath, string resxName){try{System.Diagnostics.Process.Start(@"resgen.exe", $"{txtPath} /str:cs {resxName}");}catch(Exception ex){System.Diagnostics.Debug.WriteLine(ex.Message);} }
var txt = "Hello.resource.txt"; var resx = "Hello.resx";DumpSettingsToKvTxt(Properties.Settings.Default.Properties, txt); DumpSettingsToKvTxt(Properties.Hints.Default.Properties, txt); ConvertKvTxtToResx(txt, resx);
可以將多個*.settings全部統一存到最終的*.resx,但此代碼沒有考慮到名值對重復的問題。轉換之后,直接添加.resx文件到工程即可。
將名值對轉換為C#強類型.Resx對象。 resgen hints.txt Hints.resx /str:C#,Test.Properties /publicClass
步驟:1.將Settings中的名值對提取出來;2.將名值對生成為資源文件。如下Code,提取出名值對,以key=value的形式保存在中間文件(.txt)中。該中間文件
能夠被Resgen.exe進行識別,利用resgen.exe生成C#強類型的資源文件。
*資源文件生成器 (Resgen.exe) 將文本(.txt 或 .restext)文件和基于 XML 的資源格式 (.resx) 文件轉換為公共語言運行時二進制 (.resources) 文件,后者可嵌入到運行時二進制可執行文件或附屬程序集中。
示例Code如下:
/// <summary>/// 將制定的參數集合以名值對的形式轉儲到制定的文本文件(utf-8)中。/// </summary>/// <param name="stettings">參數集合</param>/// <param name="txtPath">輸出路徑</param>/// <exception cref="System.IO.IOException"></exception>public static void DumpSettingsToKvTxt(SettingsPropertyCollection stettings, string txtPath){using (var fio = new StreamWriter(txtPath, true, Encoding.UTF8, 2048)){ foreach (var p in stettings){var sp = p as SettingsProperty;fio.WriteLine($"{sp.Name}={sp.DefaultValue.ToString()}");}fio.Flush();}}/// <summary>/// resgen Hello.resource.txt /str:cs Hello.resource.resx/// </summary>/// <param name="txtPath">名值對的txt文本</param>/// <param name="resxName">目標強類型(C#/cs)*.resx資源文件</param>public static void ConvertKvTxtToResx(string txtPath, string resxName){try{System.Diagnostics.Process.Start(@"resgen.exe", $"{txtPath} /str:cs {resxName}");}catch(Exception ex){System.Diagnostics.Debug.WriteLine(ex.Message);} }
var txt = "Hello.resource.txt"; var resx = "Hello.resx";DumpSettingsToKvTxt(Properties.Settings.Default.Properties, txt); DumpSettingsToKvTxt(Properties.Hints.Default.Properties, txt); ConvertKvTxtToResx(txt, resx);
可以將多個*.settings全部統一存到最終的*.resx,但此代碼沒有考慮到名值對重復的問題。轉換之后,直接添加.resx文件到工程即可。
將名值對轉換為C#強類型.Resx對象。 resgen hints.txt Hints.resx /str:C#,Test.Properties /publicClass
?
reference:
[1] Resgen.exe,?https://msdn.microsoft.com/zh-cn/library/ccec7sz1(v=vs.110).aspx
??
轉載于:https://www.cnblogs.com/jjseen/p/5315722.html
總結
以上是生活随笔為你收集整理的从Setting.settings到Resource.resx的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gradle初探(一):创建一个Grad
- 下一篇: 新版ADT出现appcompat_v7的