Reading From a Text File in C#

前端 未结 8 1651
轮回少年
轮回少年 2020-11-30 09:44

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 appreci

8条回答
  •  再見小時候
    2020-11-30 10:07

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    namespace part_B_19
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    StreamReader sr = new StreamReader(@"C:\Users\Acer\Documents\Visual Studio 2012\Projects\combobox.txt");
                    string line = sr.ReadLine();
    
                    while (line != null)
                    {
                        comboBox1.Items.Add(line);
                        line = sr.ReadLine();
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
    
            }
        }
    }
    

提交回复
热议问题