可以将 Byte[] 数组写入 C# 中的文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/381508/
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-04 01:21:28  来源:igfitidea点击:

Can a Byte[] Array be written to a file in C#?

c#.net

提问by Roberto Bonini

I'm trying to write out a Byte[]array representing a complete file to a file.

我正在尝试将Byte[]表示完整文件的数组写出 到文件中。

The original file from the client is sent via TCP and then received by a server. The received stream is read to a byte array and then sent to be processed by this class.

来自客户端的原始文件通过 TCP 发送,然后由服务器接收。接收到的流被读取到一个字节数组,然后发送给这个类进行处理。

This is mainly to ensure that the receiving TCPClientis ready for the next stream and separate the receiving end from the processing end.

这主要是为了确保接收TCPClient为下一个流做好准备,并将接收端与处理端分开。

The FileStreamclass does not take a byte array as an argument or another Stream object ( which does allow you to write bytes to it).

所述FileStream类不采取一个字节数组作为参数或另一个流对象(它允许你写字节到它)。

I'm aiming to get the processing done by a different thread from the original ( the one with the TCPClient).

我的目标是通过与原始线程(带有 TCPClient 的线程)不同的线程来完成处理。

I don't know how to implement this, what should I try?

我不知道如何实现这一点,我应该尝试什么?

采纳答案by Kev

Based on the first sentence of the question: "I'm trying to write out a Byte[] array representing a complete fileto a file."

基于问题的第一句话:“我正在尝试将表示完整文件的 Byte[] 数组写入文件。”

The path of least resistance would be:

阻力最小的路径是:

File.WriteAllBytes(string path, byte[] bytes)

Documented here:

记录在这里:

System.IO.File.WriteAllBytes- MSDN

System.IO.File.WriteAllBytes- MSDN

回答by Mehrdad Afshari

Yep, why not?

是的,为什么不呢?

fs.Write(myByteArray, 0, myByteArray.Length);

回答by Treb

You can use a BinaryWriterobject.

您可以使用一个BinaryWriter对象。

protected bool SaveData(string FileName, byte[] Data)
{
    BinaryWriter Writer = null;
    string Name = @"C:\temp\yourfile.name";

    try
    {
        // Create a new stream to write to the file
        Writer = new BinaryWriter(File.OpenWrite(Name));

        // Writer raw data                
        Writer.Write(Data);
        Writer.Flush();
        Writer.Close();
    }
    catch 
    {
        //...
        return false;
    }

    return true;
}

Edit:Oops, forgot the finallypart... lets say it is left as an exercise for the reader ;-)

编辑:哎呀,忘了这finally部分……让我们说它留给读者作为练习;-)

回答by JoshBerke

You can do this using System.IO.BinaryWriterwhich takes a Stream so:

您可以使用System.IO.BinaryWriterwhich 接受 Stream来执行此操作:

var bw = new BinaryWriter(File.Open("path",FileMode.OpenOrCreate);
bw.Write(byteArray);

回答by Andrew Rollings

There is a static method System.IO.File.WriteAllBytes

有一个静态方法 System.IO.File.WriteAllBytes

回答by Mitchel Sellers

You can use the FileStream.Write(byte[] array, int offset, int count)method to write it out.

您可以使用FileStream.Write(byte[] array, int offset, int count)方法将其写出。

If your array name is "myArray" the code would be.

如果您的数组名称为“myArray”,则代码为。

myStream.Write(myArray, 0, myArray.count);

回答by PatsonLeaner

Try BinaryReader:

试试 BinaryReader:

/// <summary>
/// Convert the Binary AnyFile to Byte[] format
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static byte[] ConvertANYFileToBytes(HttpPostedFileBase image)
{
    byte[] imageBytes = null;
    BinaryReader reader = new BinaryReader(image.InputStream);
    imageBytes = reader.ReadBytes((int)image.ContentLength);
    return imageBytes;
}