私のWPFアプリ「Images」には、ビルドアクションがリソースに設定されたいくつかの.pngファイルがあるフォルダーがあります。これらは XAML で参照できるため、バイナリに組み込まれています。
これらを一時フォルダーのディスクに書き込みたいと思います。どうすればいいですか?
埋め込みリソースに関するいくつかの回答を見つけましたが、単純なリソースだけではありません。
答え!
public static void ExtractFileFromResources(String filename, String location)
{
StreamResourceInfo sri = System.Windows.Application.GetResourceStream(
new Uri("pack://application:,,,/Images/" + filename));
Stream resFilestream = sri.Stream;
if (resFilestream != null)
{
BinaryReader br = new BinaryReader(resFilestream);
FileStream fs = new FileStream(location, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
bw.Write(ba);
br.Close();
bw.Close();
resFilestream.Close();
}
}