首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在C#中读取文本文件

在C#中读取文本文件
EN

Stack Overflow用户
提问于 2011-11-02 20:39:37
回答 8查看 73.9K关注 0票数 33

我使用以下程序将信息发送(输出)到文本文件,但现在我想从文本文件中读取(输入)信息。任何建议都将不胜感激。我已经注释掉了“我认为”我需要做的几件事;但我真的不确定如何继续。

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 8

Stack Overflow用户

发布于 2011-11-02 20:51:47

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

代码语言:javascript
复制
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.
    }
}

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

代码语言:javascript
复制
myTextBox.Text = File.ReadAllText(fileName);
票数 63
EN

Stack Overflow用户

发布于 2014-07-18 09:24:54

这只是一行代码:

代码语言:javascript
复制
string content = System.IO.File.ReadAllText(@"C:\textfile.txt");
票数 22
EN

Stack Overflow用户

发布于 2011-11-02 20:46:54

试试这个:

代码语言:javascript
复制
if(result == DialogResult.OK && fileName != null)
{
    try
    {
        var fileText=File.ReadAllText(fileName);
    }
    catch(Exception ex)
    {
        //Handle exception here
    }
}

它会将选定文件中的所有数据读取到fileText变量中。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7980456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档