C# 将字节数组保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19455347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:02:21  来源:igfitidea点击:

Save byte array to file

c#filestreamsavebyte

提问by Daniel M?ller

I have a byte array (an IEnumerable actually), and I need to save it to a new file containing this data.

我有一个字节数组(实际上是一个 IEnumerable),我需要将它保存到一个包含这些数据的新文件中。

How do I do that?

我怎么做?

I found some answers telling how to create a MemoryStream from that, but still can't save it to a brand new file.

我找到了一些告诉如何从中创建 MemoryStream 的答案,但仍然无法将其保存到一个全新的文件中。

采纳答案by Mike Christensen

You can use:

您可以使用:

File.WriteAllBytes("Foo.txt", arrBytes); // Requires System.IO

If you have an enumerable and not an array, you can use:

如果你有一个可枚举而不是一个数组,你可以使用:

File.WriteAllBytes("Foo.txt", arrBytes.ToArray()); // Requires System.Linq

回答by Anirudha

You can use File.WriteAllBytes

您可以使用File.WriteAllBytes