110,535
社区成员
发帖
与我相关
我的任务
分享
m_streamReader.Close();
m_streamReader.Dispose();
fs.Close();
fs.Dispose();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace mytest
{
public partial class Form4 : Form
{
string strpath = @"C:\student.txt";//设定要打开的文件路径
string strLine;
int line = 0;
public Form4()
{
InitializeComponent();
}
//当下拉框中内容改变时
private void cmbStudent_TextChanged(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream(strpath, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
while ((strLine = sr.ReadLine()) != null && strLine.Length > 0)
{
string[] spli = strLine.Split('|');
if (cmbStudent.Text == spli[0])
{
txtName.Text = spli[0];
txtYuwen.Text = spli[1];
txtShuxue.Text = spli[2];
txtYingyu.Text = spli[3];
return;
}
line++;
}
sr.Dispose();
sr.Close();
fs.Dispose();
fs.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//窗体载入时
private void Form4_Load(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream(strpath, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
while ((strLine = sr.ReadLine()) != null && strLine.Length > 0)
{
string[] spli = strLine.Split('|');
cmbStudent.Items.Add(spli[0]);
//cmbStudent.Items.Insert(line, spli[0]);
//MessageBox.Show(strLine);
line++;
}
cmbStudent.SelectedIndex = 0;
sr.Dispose();
sr.Close();
fs.Dispose();
fs.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
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;
using System.Collections;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int line = 0;
string strLine="";
FileStream fs;
OpenFileDialog openf = new OpenFileDialog();
openf.InitialDirectory = "c:\\";
openf.Filter = "exe files (*.txt)|*.txt";
openf.FilterIndex = 2;
openf.RestoreDirectory = true;
if (openf.ShowDialog() == DialogResult.OK)
{
try
{
fs = new FileStream(openf.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
do
{
strLine = m_streamReader.ReadLine();
line++;
} while (strLine != null && strLine.Length > 0);
string[,] array = new string[line, 4]; // 声明输出数组
m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
line = 0;
do
{
strLine = m_streamReader.ReadLine();
if (strLine!=null && strLine.Contains('|'))
{
string[] spli = strLine.Split('|');
array[line, 0] = spli[0];
array[line, 1] = spli[1];
array[line, 2] = spli[2];
array[line, 3] = spli[3];
line++;
}
} while (strLine != null && strLine.Length > 0);
string temp = "";
for (int i = 0; i < line; i++)
{
temp = temp + array[i, 0];
temp = temp + "|" + array[i, 1];
temp = temp + "|" + array[i, 2];
temp = temp + "|" + array[i, 3] + "\n";
}
MessageBox.Show(temp);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}