Read a file from a resource and write it to disk in C#

前端 未结 3 471
无人共我
无人共我 2021-01-22 05:32

I have some files, which are embedded in a resource. How can I save these files on disk via C#?

3条回答
  •  [愿得一人]
    2021-01-22 06:00

    You can get the resource stream, then read from the stream while writing to the file.

            byte[] buffer = new byte[1024];
            using (Stream output = File.OpenWrite("filename"))
            {
                using (Stream resourceStream = typeof(Class1).Assembly.GetManifestResourceStream("name of resource"))
                {
                    int bytes = -1;
                    while ((bytes = resourceStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        output.Write(buffer, 0, bytes);
                    }
                }
            }
    

    Code is completely untested, just to give you an idea.

提交回复
热议问题