读写二进制文件
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
// 添加新的命名空间。
8
using System.IO;
9
using System.Text;
10![]()
11
namespace BinaryFile
12
{
13
/// <summary>
14
/// 处理二进制文件。
15
/// </summary>
16
public class Form1 : System.Windows.Forms.Form
17
{
18
private System.Windows.Forms.Button button1;
19
private System.Windows.Forms.RichTextBox richTextBox1;
20
/// <summary>
21
/// 必需的设计器变量。
22
/// </summary>
23
private System.ComponentModel.Container components = null;
24![]()
25
public Form1()
26
{
27
// Windows 窗体设计器支持所必需的
28
InitializeComponent();
29
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
30
// 写二进制文件
31
s = File.Create("test.bin");
32
w = new BinaryWriter(s);
33
string str = "这是一行文字。\n";
34
w.Write(str);
35
float a = 3.1415F;
36
w.Write(a);
37
ulong b = 100000L;
38
w.Write(b);
39
int c = 300;
40
w.Write(c);
41
decimal d = 4.40983M;
42
w.Write(d);
43
double f = 94853.938485928d;
44
w.Write(f);
45
char[] g = {'h','e','l','l','o'};
46
w.Write(g, 0, g.Length);
47
char h = 'W';
48
w.Write(h);
49
bool i = true;
50
w.Write(i);
51
w.Flush();
52
w.Close();
53
}
54![]()
55
/// <summary>
56
/// 清理所有正在使用的资源。
57
/// </summary>
58
protected override void Dispose( bool disposing )
59
{
60
if( disposing )
61
{
62
if (components != null)
63
{
64
components.Dispose();
65
}
66
}
67
base.Dispose( disposing );
68
}
69![]()
70
Windows Form Designer generated code
116![]()
117
/// <summary>
118
/// 应用程序的主入口点。
119
/// </summary>
120
[STAThread]
121
static void Main()
122
{
123
Application.Run(new Form1());
124
}
125
// 定义私有变量
126
public Stream s;
127
// 读二进制文件
128
public BinaryReader r;
129
// 写二进制文件
130
public BinaryWriter w;
131
// 显示二进制文件内容
132
private void button1_Click(object sender, System.EventArgs e)
133
{
134
s = File.OpenRead("test.bin");
135
r = new BinaryReader(s);
136
richTextBox1.Text = "显示 String:";
137
richTextBox1.Text += r.ReadString();
138
richTextBox1.Text += "显示 Float:";
139
richTextBox1.Text += r.ReadSingle().ToString() + "\n";
140
richTextBox1.Text += "显示 ULong:";
141
richTextBox1.Text += r.ReadUInt64().ToString() + "\n";
142
richTextBox1.Text += "显示 Int:";
143
richTextBox1.Text += r.ReadInt32().ToString() + "\n";
144
richTextBox1.Text += "显示 Decimal:";
145
richTextBox1.Text += r.ReadDecimal().ToString() + "\n";
146
richTextBox1.Text += "显示 Double:";
147
richTextBox1.Text += r.ReadDouble().ToString() + "\n";
148
richTextBox1.Text += "显示 Char[]:";
149
richTextBox1.Text += Encoding.ASCII.GetString(r.ReadBytes(5)) + "\n";
150
richTextBox1.Text += "显示 Char:";
151
richTextBox1.Text += r.ReadChar().ToString() + "\n";
152
richTextBox1.Text += "显示 Boolean:";
153
richTextBox1.Text += r.ReadBoolean().ToString() + "\n";
154
r.Close();
155
}
156
}
157
}
158![]()
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7
// 添加新的命名空间。8
using System.IO;9
using System.Text;10

11
namespace BinaryFile12
{13
/// <summary>14
/// 处理二进制文件。15
/// </summary>16
public class Form1 : System.Windows.Forms.Form17
{18
private System.Windows.Forms.Button button1;19
private System.Windows.Forms.RichTextBox richTextBox1;20
/// <summary>21
/// 必需的设计器变量。22
/// </summary>23
private System.ComponentModel.Container components = null;24

25
public Form1()26
{27
// Windows 窗体设计器支持所必需的28
InitializeComponent();29
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码30
// 写二进制文件31
s = File.Create("test.bin");32
w = new BinaryWriter(s);33
string str = "这是一行文字。\n";34
w.Write(str);35
float a = 3.1415F;36
w.Write(a);37
ulong b = 100000L;38
w.Write(b);39
int c = 300;40
w.Write(c);41
decimal d = 4.40983M;42
w.Write(d);43
double f = 94853.938485928d;44
w.Write(f);45
char[] g = {'h','e','l','l','o'};46
w.Write(g, 0, g.Length);47
char h = 'W';48
w.Write(h);49
bool i = true;50
w.Write(i);51
w.Flush();52
w.Close();53
}54

55
/// <summary>56
/// 清理所有正在使用的资源。57
/// </summary>58
protected override void Dispose( bool disposing )59
{60
if( disposing )61
{62
if (components != null) 63
{64
components.Dispose();65
}66
}67
base.Dispose( disposing );68
}69

70
Windows Form Designer generated code116

117
/// <summary>118
/// 应用程序的主入口点。119
/// </summary>120
[STAThread]121
static void Main() 122
{123
Application.Run(new Form1());124
}125
// 定义私有变量126
public Stream s;127
// 读二进制文件128
public BinaryReader r;129
// 写二进制文件130
public BinaryWriter w;131
// 显示二进制文件内容132
private void button1_Click(object sender, System.EventArgs e)133
{134
s = File.OpenRead("test.bin");135
r = new BinaryReader(s);136
richTextBox1.Text = "显示 String:";137
richTextBox1.Text += r.ReadString();138
richTextBox1.Text += "显示 Float:";139
richTextBox1.Text += r.ReadSingle().ToString() + "\n";140
richTextBox1.Text += "显示 ULong:";141
richTextBox1.Text += r.ReadUInt64().ToString() + "\n";142
richTextBox1.Text += "显示 Int:";143
richTextBox1.Text += r.ReadInt32().ToString() + "\n";144
richTextBox1.Text += "显示 Decimal:";145
richTextBox1.Text += r.ReadDecimal().ToString() + "\n";146
richTextBox1.Text += "显示 Double:";147
richTextBox1.Text += r.ReadDouble().ToString() + "\n";148
richTextBox1.Text += "显示 Char[]:";149
richTextBox1.Text += Encoding.ASCII.GetString(r.ReadBytes(5)) + "\n";150
richTextBox1.Text += "显示 Char:";151
richTextBox1.Text += r.ReadChar().ToString() + "\n";152
richTextBox1.Text += "显示 Boolean:";153
richTextBox1.Text += r.ReadBoolean().ToString() + "\n";154
r.Close();155
}156
}157
}158



浙公网安备 33010602011771号