C# 操作文件常用的方法
作者:互联网
/// <summary> /// 文件转byte[] /// </summary> /// <param name="Path">绝对路径</param> /// <returns>byte[]</returns> public static byte[] FileToBytes(string Path) { if (!System.IO.File.Exists(Path)) return new byte[0]; FileInfo fi = new FileInfo(Path); byte[] buff = new byte[fi.Length]; FileStream fs = fi.OpenRead(); fs.Read(buff, 0, Convert.ToInt32(fs.Length)); fs.Close(); return buff; } /// <summary> /// 文件转Base64 /// </summary> /// <param name="Path">绝对路径</param> /// <returns>Base64</returns> public static string FileToBase64(string Path) { if (!System.IO.File.Exists(Path)) return string.Empty; FileInfo fi = new FileInfo(Path); byte[] buff = new byte[fi.Length]; FileStream fs = fi.OpenRead(); fs.Read(buff, 0, Convert.ToInt32(fs.Length)); fs.Close(); return BytesToBase64(buff); } /// <summary> /// byte[]转Base64 /// </summary> /// <param name="buff">byte[]</param> /// <returns>转Base64</returns> public static string BytesToBase64(byte[] buff) { return System.Text.Encoding.UTF8.GetString(buff); } /// <summary> /// Base64转byte[] /// </summary> /// <param name="Base64">转Base64</param> /// <returns>byte[]</returns> public static byte[] Base64ToBytes(string Base64) { return System.Text.Encoding.UTF8.GetBytes(Base64); } /// <summary> /// byte[]转文件保存指定地址 /// </summary> /// <param name="buff">byte数组</param> /// <param name="SavePath">保存地址</param> /// <param name="FileName">文件名</param> /// <param name="Open">是否打开文件</param> public static void BytesToFile(byte[] buff, string SavePath, string FileName, bool Open = false) { //如果不存在就创建Enclosure文件夹 if (Directory.Exists(SavePath) == false) Directory.CreateDirectory(SavePath); if (System.IO.File.Exists(SavePath + @"\" + FileName)) System.IO.File.Delete(SavePath + @"\" + FileName); FileStream fs = new FileStream(SavePath + @"\" + FileName, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(fs); bw.Write(buff, 0, buff.Length); bw.Close(); fs.Close(); if (Open) { //创建要运行的文件或者程序 var startfile = new ProcessStartInfo { FileName = SavePath + @"\" + FileName,//文件完全路径 WindowStyle = ProcessWindowStyle.Normal,//Windows窗口样式 UseShellExecute = true//为true,则用默认的打开方式打开 }; //创建Process命令 var cmd = new Process(); cmd.StartInfo = startfile; cmd.Start(); //打开文件 } } /// <summary> /// Base64转文件保存指定地址 /// </summary> /// <param name="Base64">Base64</param> /// <param name="SavePath">保存地址</param> /// <param name="FileName">文件名</param> public static void Base64ToFile(string Base64, string SavePath, string FileName, bool Open = false) { //如果不存在就创建Enclosure文件夹 if (Directory.Exists(SavePath) == false) Directory.CreateDirectory(SavePath); if (System.IO.File.Exists(SavePath + @"\" + FileName)) System.IO.File.Delete(SavePath + @"\" + FileName); byte[] buff = Base64ToBytes(Base64); FileStream fs = new FileStream(SavePath + @"\" + FileName, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(fs); bw.Write(buff, 0, buff.Length); bw.Close(); fs.Close(); if (Open) { //创建要运行的文件或者程序 var startfile = new ProcessStartInfo { FileName = SavePath + @"\" + FileName,//文件完全路径 WindowStyle = ProcessWindowStyle.Normal,//Windows窗口样式 UseShellExecute = true//为true,则用默认的打开方式打开 }; //创建Process命令 var cmd = new Process(); cmd.StartInfo = startfile; cmd.Start(); //打开文件 } } /// <summary> /// Stream转File /// </summary> /// <param name="stream">Stream</param> /// <param name="fileName">绝对路径</param> public static void StreamToFile(Stream stream, string SavePath, string FileName, bool Open = false) { //如果不存在就创建Enclosure文件夹 if (Directory.Exists(SavePath) == false) Directory.CreateDirectory(SavePath); if (System.IO.File.Exists(SavePath + @"\" + FileName)) System.IO.File.Delete(SavePath + @"\" + FileName); // 把 Stream 转换成 byte[] byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); // 把 byte[] 写入文件 FileStream fs = new FileStream(SavePath + @"\" + FileName, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(bytes); bw.Close(); fs.Close(); if (Open) { //创建要运行的文件或者程序 var startfile = new ProcessStartInfo { FileName = SavePath + @"\" + FileName,//文件完全路径 WindowStyle = ProcessWindowStyle.Normal,//Windows窗口样式 UseShellExecute = true//为true,则用默认的打开方式打开 }; //创建Process命令 var cmd = new Process(); cmd.StartInfo = startfile; cmd.Start(); //打开文件 } } /// <summary> /// byte[]转System.Drawing.Image /// </summary> /// <param name="buffer">byte[]</param> /// <returns>System.Drawing.Image</returns> public static Image ByteToImage(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close(); return img; } /// <summary> /// System.Drawing.Bitmap转byte[] /// </summary> /// <param name="Bit">System.Drawing.Bitmap</param> /// <param name="format">图片格式</param> /// <returns>byte[]</returns> public static byte[] ByteToBitmap(Bitmap Bitmap, ImageFormat format) { byte[] back = null; MemoryStream ms = new MemoryStream(); Bitmap.Save(ms, format); back = ms.GetBuffer(); ms.Dispose(); return back; } /// <summary> /// byte[]转内存流MemoryStream /// </summary> /// <param name="buff">byte[]</param> /// <returns>MemoryStream</returns> public static MemoryStream ByteToMemoryStream(byte[] buff) { return new MemoryStream(buff); } /// <summary> /// 内存流MemoryStream转byte[] /// </summary> /// <param name="outStream">MemoryStream</param> /// <returns>byte[]</returns> public static byte[] MemoryStreamToByte(MemoryStream outStream) { return outStream.ToArray(); } /// <summary> /// 流Stream转byte[] /// </summary> /// <param name="stream">Stream</param> /// <returns>byte[]</returns> public byte[] StreamToByte(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; } /// <summary> /// byte[]转Stream流 /// </summary> /// <param name="bytes">byte[]</param> /// <returns>Stream</returns> public Stream ByteToStream(byte[] bytes) { return new MemoryStream(bytes); } //从文件读取 Stream /// <summary> /// 文件转Stream流 /// </summary> /// <param name="FileName">绝对路径</param> /// <returns>Stream流</returns> public Stream FileToStream(string FileName) { // 打开文件 FileStream fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); // 读取文件的 byte[] byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Dispose(); fileStream.Close(); // 把 byte[] 转换成 Stream Stream stream = new MemoryStream(bytes); return stream; }
标签:文件,常用,fs,Stream,C#,FileName,SavePath,new,byte 来源: https://www.cnblogs.com/yy15611/p/13370559.html