编程题
(上机实验题在最后!)
----------------------------------------------分割线------------------------------------------------------------------------------
存储
存储----存储----存储

// An highlighted block
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 exci12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = "F:\\MyTest1.txt";//文件名
if (File.Exists(path))
File.Delete(path);
else
{
FileStream fs = File.OpenWrite(path);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
sw.WriteLine(textBox1.Text);
sw.Close();
fs.Close();
}
}
}
}
读取

// An highlighted block
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 exci12
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = "F:\\MyTest1.txt";//文件名
string mystr = "";
FileStream fs = File.OpenRead(path);
StreamReader sr = new StreamReader(fs, Encoding.Default);
//指定打开文件
//将文件流指针定位在开始位置
fs.Seek(0,SeekOrigin.Begin);
while (sr.Peek() > -1)
mystr = mystr + sr.ReadLine() + "/r/n";
sr.Close();
fs.Close();
textBox1.Text = mystr;
}
}
}
上机实验题

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
{
string path = "D:\\MyTest1.txt";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (File.Exists(path))
File.Delete(path);
else
{
StreamWriter sw = File.CreateText(path);
sw.Close();
FileStream fs = File.OpenWrite(path);
StreamWriter sb = new StreamWriter(fs, Encoding.Default);
sb.WriteLine(textBox1.Text);
sb.WriteLine(textBox2.Text);
sb.WriteLine(textBox3.Text);
sb.WriteLine(textBox4.Text);
sb.WriteLine(textBox5.Text);
sb.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
int i;
string mystr = "学号\t姓名\t性别\t年龄\t分数\r\n";
FileStream fs = File.OpenRead(path);
StreamReader sr = new StreamReader(fs, Encoding.Default);
fs.Seek(0, SeekOrigin.Begin);
while (sr.Peek() > -1)
{
for (i = 0; i < 5; i++)
mystr = mystr + sr.ReadLine() + "\t";
mystr = mystr + "\r\n";
}
sr.Close();
fs.Close();
textBox6.Text = mystr;
}
}
}
7713



被折叠的 条评论
为什么被折叠?



