Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is best to Use for creating a file from byte[]?

Tags:

c#

.net

file

I want to create a file using byte[], which one is best.

byte[] content=File.ReadAllBytes(@"C:\ServiceLog.txt");

FileStream stream = new FileStream(@"C:\ServiceLog1.txt", FileMode.Create, FileAccess.ReadWrite);
stream.Write(content, 0, content.Length);
stream.Close();

or

 File.WriteAllBytes(@"C:\12.txt",content);
like image 337
Kishore Kumar Avatar asked Jul 20 '12 09:07

Kishore Kumar


1 Answers

If you have a byte[], you might as well just use WriteAllBytes and let the wrapper method worry about the rest. The Stream approach is useful when you are (oddly enough) streaming the data, meaning: you might not know it all when you start writing. Since you do have the byte[], just us it. However, in the general case, note that byte[]-based APIs may have more impact on memory than Stream-based APIs, especially for large content.

like image 104
Marc Gravell Avatar answered Sep 20 '22 06:09

Marc Gravell