参考https://blog.csdn.net/iloveyou00003/article/details/77478277
https://blog.csdn.net/ymnl_gsh/article/details/80723050
https://blog.csdn.net/Hana_one/article/details/83833479
https://blog.csdn.net/e295166319/article/details/52704080
https://bbs.csdn.net/topics/390121874(这篇博客和我遇到的问题一致)
https://blog.csdn.net/xpj8888/article/details/84999413 C# 基础(十二)C# 文本文件的读写、清零,将文本文件的内容读到字符串:主要应用在写日志
一、问题描述
很显然,上述的几篇博客,大概讲述了Filestream的读写、BinaryReader的读写。其实,在C#中,我们常常可以用BinaryReader/Writer类、StreamReader/Writer类、filestream类、File类读写文件及其区别。
1.1、filestream读数据和BinaryReader读的数据,是一样的,但是BinaryReader还可以读取成字符串等等格式。这就是BinaryReader的优势。
byte[] bytPictureContent = new byte[0]; //图片内容
byte[] bytPictureContent2 = new byte[0]; //图片内容
/// <summary>
/// 图片转字节
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
private byte[] ImageConvertToByte(string Path)
{
byte[] bytContent = new byte[0];
try
{
FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
bytContent = new byte[fs.Length];
fs.Read(bytContent, 0, Convert.ToInt32(fs.Length));
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show("SendPackageFunc()——图片转字节发生错误" + ex.ToString());
}
return bytContent;
}
private byte[] ImageConvertToByte2(string Path)
{
byte[] bytContent = new byte[0];
try
{
using (BinaryReader reader = new BinaryReader(File.Open(Path, FileMode.Open)))
{
//reader.Read(bytContent, 0, bytContent.Length);
bytContent = reader.ReadBytes(bytPictureContent.Length);
}
}
catch (Exception ex)
{
MessageBox.Show("SendPackageFunc()——图片转字节发生错误" + ex.ToString());
}
return bytContent;
}