おでんはじめました。

required ちくわぶ and 巾着,optional はんぺん.

C#でリソースファイルからテキストを取得する

いつも忘れてしまうのでメモ。

VSでファイルを追加してプロパティのビルドアクションを「埋め込まれたリソース」にするのを忘れないこと。

//現在のコードを実行しているAssemblyを取得
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();

//ネームスペースがkj.sample、リソース名がtest.xmlの場合
System.IO.StreamReader sr = new System.IO.StreamReader(myAssembly.GetManifestResourceStream("kj.sample.test.xml"), Encoding.UTF8);
if (sr == null) throw new Exception("リソースを取得できない");
string s = sr.ReadToEnd();
sr.Close();

リソースの一覧の取得方法もメモ。

//指定されたマニフェストリソースを読み込む
string[] resnames=myAssembly.GetManifestResourceNames();
foreach (string res in resnames) {
  Console.WriteLine("resource {0}",res);
}