欢迎光临
我的个人博客网站

C# 文件流操作

如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

C# 文件流操作

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;  using System.IO;  namespace WindowsFormsApp10 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }          private void Form1_Load(object sender, EventArgs e)         {             if (!Directory.Exists(Environment.CurrentDirectory + "\bin"))                    Directory.CreateDirectory(Environment.CurrentDirectory + "\bin");             // FileStream f = File.Create(Environment.CurrentDirectory + "\bin\mydata.bin");            // f.Close();           //  f.Dispose();              if (!File.Exists(Environment.CurrentDirectory + "\bin\mydata.bin"))             File.Create(Environment.CurrentDirectory + "\bin\mydata.bin");          }         private void writerFile(byte[] array, string strPath)         {             //string content = this.txtContent.Text.ToString();              if (string.IsNullOrEmpty(strPath))             {                 return;             }              //将string转为byte数组             //byte[] array = Encoding.UTF8.GetBytes(content);              //string path = Server.MapPath("/test.txt");             //创建一个文件流                          FileStream fs = new FileStream(strPath, FileMode.Create);                  //将byte数组写入文件中                 fs.Write(array, 0, array.Length);                 //所有流类型都要关闭流,否则会出现内存泄露问题                 fs.Close();                 //Response.Write("保存文件成功");                      }          private void button1_Click(object sender, EventArgs e)         {             Byte[] recv = new Byte[] { 0x01, 0x02, 0x03 };             writerFile(recv, Environment.CurrentDirectory + "\bin\mydata.bin");             MessageBox.Show("数据写入完成!");         }     } } 

  C# 文件流操作

 

 做如下修改:

private void Form1_Load(object sender, EventArgs e)         {             if (!Directory.Exists(Environment.CurrentDirectory + "\bin"))                    Directory.CreateDirectory(Environment.CurrentDirectory + "\bin");             FileStream f = File.Create(Environment.CurrentDirectory + "\bin\mydata.bin");             f.Close();            f.Dispose();              //if (!File.Exists(Environment.CurrentDirectory + "\bin\mydata.bin"))             //File.Create(Environment.CurrentDirectory + "\bin\mydata.bin");          } 

  运行结果:

C# 文件流操作

 

 文件写入:

if (!Directory.Exists(Environment.CurrentDirectory + "\bin")) Directory.CreateDirectory(Environment.CurrentDirectory + "\bin");  if (!File.Exists(Environment.CurrentDirectory + "\bin\mydata.bin"))   File.Create(Environment.CurrentDirectory + "\bin\mydata.bin");       lock (ThreadLock)     {  FileStream fs = new FileStream(Environment.CurrentDirectory + "\bin\mydata.bin", FileMode.Append,FileAccess.Write);//FileAccess.Write必须的   fs.Write(recv, 0, recv.Length);   fs.Close();    fs.Dispose();      }                 

  

赞(0) 打赏
未经允许不得转载:张拓的天空 » C# 文件流操作
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏