Administrateur
Administrateur

Reputation: 901

File.Copy a file created in memory

So I create this file:

MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms);
tw.WriteLine("HELLO WORLD!");
tw.Flush();
byte[] bytes = ms.GetBuffer();

How can I use File.Copy() to, well, copy this file to a new file?

Upvotes: 1

Views: 402

Answers (2)

Selman Genç
Selman Genç

Reputation: 101681

Since you have your bytes, you can write them to a new file using File.WriteAllBytes method:

File.WriteAllBytes("path", bytes);

If the only matter is writing some text content to a file, I would recommend you to use File.WriteAllText method:

File.WriteAllText("path", "HELLO WORLD");

Upvotes: 5

Michael Liu
Michael Liu

Reputation: 55339

Use File.WriteAllBytes to create a new file (or overwrite an existing file) from a byte array:

File.WriteAllBytes(fileName, bytes);

Note that a MemoryStream (what you created) really isn't a "file", so File.Copy can't be used.

Upvotes: 3

Related Questions