Created
November 9, 2014 02:18
-
-
Save jugemjugem/f7a70939a51d7a577b57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| using System.Resources; | |
| namespace TestExe | |
| { | |
| static class Program | |
| { | |
| // 自分用リソースデータのタイプ文字列。 | |
| private const string MyResString = "MyString"; | |
| // dotnet が利用しているリソースデータ(String)用のタイプ文字列。 | |
| // http://referencesource.microsoft.com/#mscorlib/system/resources/resourcetypecode.cs,53dd846b92cd66bd | |
| private const string DotnetResString = "ResourceTypeCode.String"; | |
| // リソースのキー | |
| private const string KeyType1 = "key1"; | |
| private const string KeyType2 = "key2"; | |
| // 実験用に作成する *.resources ファイル名 | |
| private const string ResoruceFileName = "myResources.resources"; | |
| private const string TestData1 = "Value--1-input"; | |
| private const string TestData2 = "入力データのテストーーーとても長い文字列" + | |
| "ゆうていみやおうきむこうほりいゆうじとりやまあきらぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺああああ" + | |
| "ゆうていみやおうきむこうほりいゆうじとりやまあきらぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺいいいい" + | |
| "ゆうていみやおうきむこうほりいゆうじとりやまあきらぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺうううう" + | |
| "ゆうていみやおうきむこうほりいゆうじとりやまあきらぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺええええ" + | |
| "ゆうていみやおうきむこうほりいゆうじとりやまあきらぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺおおおお" + | |
| "ゆうていみやおうきむこうほりいゆうじとりやまあきらぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺぺかかかか" | |
| ; | |
| private static void Main(string[] args) | |
| { | |
| // myResources.resources なるリソースファイルを作成する。 | |
| using (var rw = new ResourceWriter(ResoruceFileName)) | |
| { | |
| // KeyType1 をキーに持ち、独自データ形式で文字を追加。 | |
| rw.AddResourceData(KeyType1, MyResString, System.Text.Encoding.UTF8.GetBytes(TestData1)); | |
| // KeyType2 をキーに持つような文字データを追加する。 | |
| rw.AddResource(KeyType2, TestData2); | |
| // Dispose すれば明示的に呼び出さなくてもいいのかな? | |
| //rw.Generate(); | |
| } | |
| // 書きこんだデータを読みだしてみる。 | |
| using (var rr = new ResourceReader(ResoruceFileName)) | |
| { | |
| // ResourceReader だと、 GetResourceData しかメソッドが無いらしい。 | |
| // 本来は、 ResourceManager から読み込んでもらうので、便利なメソッドはないもよう。 | |
| string type; | |
| byte[] data; | |
| // 独自データ形式のリソースを読み込む。 | |
| rr.GetResourceData(KeyType1, out type, out data); | |
| // データタイプを表す文字は、自分で追加したときの文字列のはず。 | |
| System.Diagnostics.Debug.Assert(type == MyResString); | |
| string text1 = System.Text.Encoding.UTF8.GetString(data); | |
| System.Console.WriteLine("reload string:{0}, {1}", KeyType1, text1); | |
| System.Diagnostics.Debug.Assert(TestData1 == text1); | |
| // AddResource(string,string) で追加したデータを読み込む。 | |
| rr.GetResourceData(KeyType2, out type, out data); | |
| // AddResource(string,string) で追加した場合、データタイプを表す文字は"ResourceTypeCode.String" らしい。 | |
| System.Diagnostics.Debug.Assert(type == DotnetResString); | |
| using (var fs = new MemoryStream(data)) | |
| { | |
| var rd = new BinaryReader(fs); | |
| var text = rd.ReadString(); | |
| System.Console.WriteLine("reload string:{0}, {1}", KeyType2, text); | |
| System.Diagnostics.Debug.Assert(TestData2 == text); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment