从文本文件中读取C#

编程入门 行业动态 更新时间:2024-10-24 08:22:02
本文介绍了从文本文件中读取C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下程序将发送(输出)信息到文本文件,但现在我想从文本文件中读取(输入)。任何建议将非常感谢。我已经注释了一些我认为我需要做的事情;但我不确定如何继续。

使用System.Windows.Forms; using System.IO; 命名空间Input_Output { public partial class成绩:表单 {私人StreamWriter输出; private StreamReader输入; public Grades() { InitializeComponent(); } private void label4_Click(object sender,EventArgs e) { } private void btnCreate_Click sender,EventArgs e) { btnEnter.Visible = true; btnClose.Visible = true; txtFirst.Visible = true; txtLast.Visible = true; lblFirst.Visible = true; lblLast.Visible = true; listBox1.Visible = true; lblStatus.Visible = true; lblGrade.Visible = true; lblCourse.Visible = true; cmbID.Visible = true; lblID.Visible = true; txtCourse.Visible = true; txtGrade.Visible = true; lblStatus.Visible = true; DialogResult result; string fileName; using(SaveFileDialog chooser = new SaveFileDialog()) { result = chooser.ShowDialog(); fileName = Chooser.FileName; } output = new StreamWriter(fileName); btnCreate.Enabled = false; txtFirst.Visible = true; txtLast.Visible = true; lblFirst.Visible = true; lblLast.Visible = true; btnEnter.Visible = true; btnClose.Visible = true; } private void btnClose_Click(object sender,EventArgs e) { //关闭按钮将列表框中的信息推送到文本文件 output.Close(); lblStatus.ForeColor = Color.Red; lblStatus.Text =输出文件; btnCreate.Enabled = true; listBox1.Items.Clear(); cmbID.Text =; } private void btnEnter_Click(object sender,EventArgs e) { //输入按钮发送信息到列表框,消息框提示用户检查为准确。 //关闭按钮将信息推送到文本文件。 string last =; string first =; string course=; string grade =; if(txtFirst.Text!=& txtLast.Text!=&& txtCourse.Text!=) { last = txtFirst.Text; first = txtLast.Text; course = txtCourse.Text; grade = txtGrade.Text; output.WriteLine(last +\t+\t+ first +:+\t+ cmbID.SelectedItem +_+ course +_+ grade) ; listBox1.Items.Add(txtLast.Text +,+ txtFirst.Text +:+ cmbID.SelectedItem + - + txtCourse.Text + - + txtGrade.Text) ; lblStatus.ForeColor = Color.Navy; lblStatus.Text =Entry Saved; txtFirst.Text =; txtLast.Text =; txtCourse.Text =; txtGrade.Text =; txtFirst.Focus(); } else { lblStatus.ForeColor = Color.Red; lblStatus.Text =空文本框或框; } MessageBox.Show(请在继续之前验证信息是否正确); } private void btnExit_Click(object sender,EventArgs e) { Application.Exit(); } private void Grades_Load(object sender,EventArgs e) { } private void button1_Click sender,EventArgs e) { DialogResult result; string fileName; using(OpenFileDialog chooser = new OpenFileDialog()) { result = chooser.ShowDialog(); fileName = chooser.FileName; } // while循环? //如果变量为null,它是记录的结尾 // variable =!null // txt读取int变量TxtFile.Text + = Rec +\r\ n。 while rec!= null; } } }

解决方案>

要一次读取一行文本文件,您可以这样做:

使用System.IO; 使用(var reader = new StreamReader(fileName)) { string line; while((line = reader.ReadLine())!= null) { //在这里做你的行,它将为每个 //行调用文件中的文本。 } }

还有其他方法。例如,如果文件不是太大,并且您只想将所有内容读取到单个字符串,则可以使用 File.ReadAllText()

myTextBox.Text = File.ReadAllText(fileName);

I have the following program that will send (output) information to a text file, but now I want to read (input) from the text file. Any suggestions would be greatly appreciated. I have commented out a couple of things that "I think" I need to do; but I am not really certain how to proceed.

using System.Windows.Forms; using System.IO; namespace Input_Output { public partial class Grades : Form { private StreamWriter output; private StreamReader input; public Grades() { InitializeComponent(); } private void label4_Click(object sender, EventArgs e) { } private void btnCreate_Click(object sender, EventArgs e) { btnEnter.Visible = true; btnClose.Visible = true; txtFirst.Visible = true; txtLast.Visible = true; lblFirst.Visible = true; lblLast.Visible = true; listBox1.Visible = true; lblStatus.Visible = true; lblGrade.Visible = true; lblCourse.Visible = true; cmbID.Visible = true; lblID.Visible = true; txtCourse.Visible = true; txtGrade.Visible = true; lblStatus.Visible = true; DialogResult result; string fileName; using (SaveFileDialog chooser = new SaveFileDialog()) { result = chooser.ShowDialog(); fileName = chooser.FileName; } output = new StreamWriter(fileName); btnCreate.Enabled = false; txtFirst.Visible = true; txtLast.Visible = true; lblFirst.Visible = true; lblLast.Visible = true; btnEnter.Visible = true; btnClose.Visible = true; } private void btnClose_Click(object sender, EventArgs e) { //Close button pushes information from the listbox in to the text file output.Close(); lblStatus.ForeColor = Color.Red; lblStatus.Text = "Output File"; btnCreate.Enabled = true; listBox1.Items.Clear(); cmbID.Text = ""; } private void btnEnter_Click(object sender, EventArgs e) { // Enter button sends information to the list box, a Message Box prompts user to check for accuracy. //Close button pushes information to the Text file. string last = ""; string first = ""; string course = ""; string grade = ""; if (txtFirst.Text != "" && txtLast.Text != "" && txtCourse.Text != "") { last = txtFirst.Text; first = txtLast.Text; course = txtCourse.Text; grade = txtGrade.Text; output.WriteLine (last + "\t"+ "\t" + first + ":"+ "\t" + cmbID.SelectedItem + "_" + course + "_" + grade ); listBox1.Items.Add(txtLast.Text + "," + txtFirst.Text + ":" + cmbID.SelectedItem + "-" + txtCourse.Text + "-" + txtGrade.Text); lblStatus.ForeColor = Color.Navy; lblStatus.Text = "Entry Saved"; txtFirst.Text = ""; txtLast.Text = ""; txtCourse.Text = ""; txtGrade.Text = ""; txtFirst.Focus(); } else { lblStatus.ForeColor = Color.Red; lblStatus.Text = "Empty text box or boxes"; } MessageBox.Show("Please verify that the information is correct before proceeding"); } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void Grades_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { DialogResult result; string fileName; using (OpenFileDialog chooser = new OpenFileDialog()) { result = chooser.ShowDialog(); fileName = chooser.FileName; } //while loop? //if variable is null, it's the end of the record //variable= !null //txt read int variable TxtFile.Text += Rec + "\r\n"; while rec !=null; } } }

解决方案

To read a text file one line at a time you can do like this:

using System.IO; using (var reader = new StreamReader(fileName)) { string line; while ((line = reader.ReadLine()) != null) { // Do stuff with your line here, it will be called for each // line of text in your file. } }

There are other ways as well. For example, if the file isn't too big and you just want everything read to a single string, you can use File.ReadAllText()

myTextBox.Text = File.ReadAllText(fileName);

更多推荐

从文本文件中读取C#

本文发布于:2023-11-06 05:16:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!