如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !
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("数据写入完成!"); } }}
做如下修改:
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"); }
运行结果:
文件写入:
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(); }
原文转载:http://www.shaoqun.com/a/500051.html
美菜:https://www.ikjzd.com/w/1874
myyearbook:https://www.ikjzd.com/w/726
张洁:https://www.ikjzd.com/w/1663
如果需要查看更多文章,请微信搜索公众号csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群,我邀请你进群!!!usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSy
tiki:tiki
福茂:福茂
干货:2020跨境电商营销趋势大预测与建议分享!:干货:2020跨境电商营销趋势大预测与建议分享!
义江缘景区游玩需要多长时间?桂林义江缘几个小时可以玩完?:义江缘景区游玩需要多长时间?桂林义江缘几个小时可以玩完?
2020年端午深圳世界之窗有什么活动吗?:2020年端午深圳世界之窗有什么活动吗?
No comments:
Post a Comment