C# File 文件操作
文件转Byte[]
public static byte[] GetBytesByFile(string path)
{
if (!File.Exists(path))
return null;
byte[] data = null;
using (FileStream fs_read = new FileStream(path, FileMode.Open, FileAccess.Read))
{
data = new byte[fs_read.Length];
fs_read.Read(data, 0, data.Length);
}
return data;
}
Byte转文件[]
public static void SaveFileFromBytes(byte[] data, string path)
{
if (data == null || data.Length == 0)
return;
using (FileStream fs_write = new FileStream(path, FileMode.Create, FileAccess.Write))
{
fs_write.Write(data, 0, data.Length);
}
}