Resource from assembly as a stream

前端 未结 4 1411
予麋鹿
予麋鹿 2020-12-05 02:43

I have an image in a C# WPF app whose build action is set to \'Resource\'. It\'s just a file in the source directory, it hasn\'t been added to the app\'s resource collection

4条回答
  •  感动是毒
    2020-12-05 03:10

    There's no need to call the Close() method, it will be automatically called by Dispose() at the end of the using clause. So your code might look like this:

    using(BinaryReader reader = new BinaryReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ResourceBlenderExpress.Resources.images.flags.tr.png")))
    using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(imageFile))) 
    {
        while((read = reader.Read(buffer, 0, buffer.Length)) > 0) 
        {
            writer.Write(buffer, 0, read);
        }
    }
    

提交回复
热议问题