views:

24

answers:

1

I have a folder in my WPF app "Images" that has several .png files with their Build Action set to Resource. These are built into my binary since I can reference them in XAML.

I would like to write these to disk in the temp folder. How do I do this?

I have found several answers referring to Embedded Resources, but not just plain resources.

A: 

Answer!

 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();
     }

  }
Jippers