我的 WPF 应用程序“图像”中有一个文件夹,其中有几个 .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();
}
}