基本信息
源码名称:C# 二进制文件读写示例(dat文件)
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2018-06-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static string ConfigPth;
public Form1()
{
InitializeComponent();
}
private void 二进制文件写入_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)//判断文本框的内容是不是为空
{
MessageBox.Show("要写入的文件内容不能为空");
}
else
{
//设置保存文件的格式
saveFileDialog1.Filter = "二进制文件(*.dat)|*.dat";
ConfigPth = @"123.dat";
//使用“另存为”对话框中输入的文件名实例化FileStream对象
FileStream myStream = new FileStream(ConfigPth, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//使用FileStream对象实例化BinaryWriter二进制写入流对象
BinaryWriter myWriter = new BinaryWriter(myStream);
//以二进制方式向创建的文件中写入内容
myWriter.Write(textBox1.Text);
//关闭当前二进制写入流
myWriter.Close();
//关闭当前文件流
myStream.Close();
}
}
private void 二进制文件读出_Click(object sender, EventArgs e)
{
//设置打开文件的格式
ConfigPth = @"123.dat";//获取当前路
textBox2.Text = string.Empty;
//使用“打开”对话框中选择的文件名实例化FileStream对象
FileStream myStream = new FileStream(ConfigPth, FileMode.Open, FileAccess.Read);
//使用FileStream对象实例化BinaryReader二进制写入流对象
BinaryReader myReader = new BinaryReader(myStream);
if (myReader.PeekChar() != -1)
{
//以二进制方式读取文件中的内容
textBox2.Text = Convert.ToString(myReader.ReadString());
}
//关闭当前二进制读取流
myReader.Close();
//关闭当前文件流
myStream.Close();
}
}
}