C#文件到字节数组和字节数组到文件 [英] C# file to Byte Array and Byte Array to File

查看:66
本文介绍了C#文件到字节数组和字节数组到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将文件转换为字节数组并保存在MYSQL数据库中。

我需要将字节数组转换为文件以将文件发送到另一个程序。



谢谢

I already convert the file to byte array and save in the MYSQL Database.
I need to convert the byte array to file to send the file to another program.

Thanks

推荐答案

您需要知道您的文件扩展名。我假设你要么在表中或者它的相同文件扩展名,不管是什么(比如pdf)。



1)读/写文件到字节数组并返回文件



You would need to know your file extension. Im assuming you either have it in the table or its the same file extension no matter what (say pdf).

1) Read/Write a file to a byte array and back to file

//Read file to byte array

FileStream stream = File.OpenRead(@"c:\path\to\your\file\here.txt");
byte[] fileBytes= new byte[stream.Length];

stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
//Begins the process of writing the byte array back to a file

using (Stream file = File.OpenWrite(@"c:\path\to\your\file\here.txt"))
{
   file.Write(fileBytes, 0, fileBytes.Length);
}





除非我完全误解你的问题应该是你所要求的我相信。



Unless i completely misunderstood your question that should be what you are asking for i believe.


最简单的解决方案是:

File.ReadAllBytes [ ^ ]获取byes和 File.WriteAllBytes [ ^ ]将其写回文件。
Easiest solution is:
File.ReadAllBytes[^] to get the byes and File.WriteAllBytes[^] to write it back to a file.


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


这篇关于C#文件到字节数组和字节数组到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆