如何阅读和放大器;写入二进制文件在C#? [英] How Do I Read & Write Binary Files In C#?
本文介绍了如何阅读和放大器;写入二进制文件在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想C#编写的应用程序,将数据写入到一个二进制文件,然后读取它。问题是,当我尝试读它,应用程序的错误崩溃无法读取超出流的末尾。
这里的code:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms的;
使用System.IO;命名空间Read_And_Write_To_Binary
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
} 私人无效btnSave_Click(对象发件人,EventArgs的发送)
{
SaveFileDialog SaveFileDialog =新SaveFileDialog();
SaveFileDialog.Title =另存为...;
SaveFileDialog.Filter =二进制文件(* .bin文件)| * .bin文件;
SaveFileDialog.InitialDirectory = @C:\\;
如果(SaveFileDialog.ShowDialog()== DialogResult.OK)
{
的FileStream FS =新的FileStream(SaveFileDialog.FileName,FileMode.Create);
//创建数据的作家。
的BinaryWriter体重=新的BinaryWriter(FS); 字符串名称= Convert.ToString(txtName.Text);
INT年龄= Convert.ToInt32(txtAge.Text);
bw.Write(姓名);
bw.Write(年龄); fs.Close();
bw.Close();
}
} 私人无效btnOpen_Click(对象发件人,EventArgs的发送)
{
打开文件对话框打开文件对话框=新的OpenFileDialog();
OpenFileDialog.Title =打开文件...;
OpenFileDialog.Filter =二进制文件(* .bin文件)| * .bin文件;
OpenFileDialog.InitialDirectory = @C:\\;
如果(OpenFileDialog.ShowDialog()== DialogResult.OK)
{
的FileStream FS =新的FileStream(OpenFileDialog.FileName,FileMode.Create);
BinaryReader BR =新BinaryReader(FS); lblName.Text = br.ReadString();
lblAge.Text = br.ReadInt32(); fs.Close();
br.Close();
}
}
}
}
解决方案
您正在使用 FileMode.Create
阅读该文件。
您应该使用 FileMode.Open
代替。
的FileStream FS =新的FileStream(SaveFileDialog.FileName,FileMode.Open);
当您打开流来创建文件,现有的文件将被重写,所以你会得到这个例外。这是因为文件中没有可用的数据。
I'm trying to write an app in C# that will write data to a binary file and then read it. The problem is that when I try to read it, the app crashes with the error "Unable to read beyond the end of the stream."
Here's the code:
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 Read_And_Write_To_Binary
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog SaveFileDialog = new SaveFileDialog();
SaveFileDialog.Title = "Save As...";
SaveFileDialog.Filter = "Binary File (*.bin)|*.bin";
SaveFileDialog.InitialDirectory = @"C:\";
if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(SaveFileDialog.FileName, FileMode.Create);
// Create the writer for data.
BinaryWriter bw = new BinaryWriter(fs);
string Name = Convert.ToString(txtName.Text);
int Age = Convert.ToInt32(txtAge.Text);
bw.Write(Name);
bw.Write(Age);
fs.Close();
bw.Close();
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog = new OpenFileDialog();
OpenFileDialog.Title = "Open File...";
OpenFileDialog.Filter = "Binary File (*.bin)|*.bin";
OpenFileDialog.InitialDirectory = @"C:\";
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(OpenFileDialog.FileName, FileMode.Create);
BinaryReader br = new BinaryReader(fs);
lblName.Text = br.ReadString();
lblAge.Text = br.ReadInt32();
fs.Close();
br.Close();
}
}
}
}
解决方案
You're using FileMode.Create
for reading the file.
You should use FileMode.Open
instead.
FileStream fs = new FileStream(SaveFileDialog.FileName, FileMode.Open);
When you open the stream for creating a file, the existing file will be rewritten, so you'll get this exception as there is no data available in the file.
这篇关于如何阅读和放大器;写入二进制文件在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文