C# 如何逐行读取文本文件并将这些字符串添加到数组中?

C# 如何逐行读取文本文件并将这些字符串添加到数组中?,c#,io,C#,Io,我想从文本文件中读取两个数字:2,5(在我的文本文件中,第一行是2,第二行是5)。我花了几个小时试图解决这个问题,但每次运行代码时,都会在这一行导致错误 citaj[o] = int.Parse(h); 这是我按钮的完整代码。我在逐行将其放入richtextbox时也出现了一些错误 namespace WindowsFormsApplication1 { public partial class Form1 : Form { int p = 4;

我想从文本文件中读取两个数字:2,5(在我的文本文件中,第一行是2,第二行是5)。我花了几个小时试图解决这个问题,但每次运行代码时,都会在这一行导致错误

 citaj[o] = int.Parse(h);
这是我按钮的完整代码。我在逐行将其放入richtextbox时也出现了一些错误

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int p = 4;
        int i = 0;
        int b = 0;
        int c = 0;
        int x = 0;
        TextBox[] text = new TextBox[50];
        string[] linije = new string[50];
        string[] brojac = new string[10];

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            text[1] = textBox1;
            text[2] = textBox2;
            text[3] = textBox3;
            brojac[0] = p.ToString();
            brojac[1] = c.ToString();
            System.IO.File.WriteAllLines(@"text\brojac.txt", brojac);
            if (b == 0)
            {

                for (i = c; i <= p; i++)
                {
                    if ((i == c) && (x == 0))
                    {
                        linije[i] = "---------------";
                        x = 1;
                    }

                    else if (i == p)
                    {
                        linije[i] = "------------------";
                        b = 1;
                    }
                    else
                    {
                        switch (x)
                        {
                            case 3: linije[i] ="Sifra:" + " " + text[3].Text;
                                x = 0;
                                break;
                            case 2: linije[i] ="Korisnicko ime:" + " " + text[2].Text;
                                x = 3;
                                break;
                            case 1: linije[i] ="Naziv:" + " " + text[1].Text;
                                x = 2;
                                break;
                        }
                    }

                }
            }
            if(b == 1) 
            {
                c = c + 5;
                p = p + 5;
                b = 0;
                System.IO.File.WriteAllLines(@"text\Kontener.txt", linije);
            }



        }

        private void button2_Click(object sender, EventArgs e)
        {
            int o = 0;
            string h;
            int[] citaj = new int[2];
            System.IO.StreamReader sr = new System.IO.StreamReader(@"text\brojac.txt");
            while ((h = sr.ReadLine()) != null)
            {
                string[] parts = h.Split(',');
                citaj[0] = int.Parse(parts[0]);
                citaj[1] = int.Parse(parts[1]);
            }
            richTextBox1.Lines = new string[] { citaj[0].ToString(), citaj[1].ToString() };
        }
    }
}
命名空间窗口窗体应用程序1
{
公共部分类Form1:Form
{
int p=4;
int i=0;
int b=0;
int c=0;
int x=0;
TextBox[]text=新的TextBox[50];
字符串[]linije=新字符串[50];
字符串[]brojac=新字符串[10];
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
text[1]=textBox1;
text[2]=textBox2;
text[3]=textBox3;
brojac[0]=p.ToString();
brojac[1]=c.ToString();
System.IO.File.writeAllines(@“text\brojac.txt”,brojac);
如果(b==0)
{

对于(i=c;i数组未初始化

尝试使用列表而不是数组

private void button2_Click(object sender, EventArgs e)
{
    List<int> citaj = new List<int>();
    string h;
    using(System.IO.StreamReader sr = new System.IO.StreamReader(@"text\brojac.txt")) 
    {
        while ((h = sr.ReadLine()) != null)
        {
            int number = 0;
            if (int.TryParse(h, out number)) 
                citaj.Add(number);
        } 
    } 
} 
private void按钮2\u单击(对象发送者,事件参数e)
{
List citaj=新列表();
字符串h;
使用(System.IO.StreamReader sr=new System.IO.StreamReader(@“text\brojac.txt”))
{
而((h=sr.ReadLine())!=null)
{
整数=0;
if(内锥巴色(h,输出编号))
补充(编号);
} 
} 
} 

ReadLine读取整行直到换行。如果在同一行中有2,5,则无法将2,5解析为整数。顺便说一句:在
richTextBox1.Lines[g]=citaj[g].ToString();
到达该行时也会出现异常。…
var yourArr=File.ReadAllLines(@“text\brojac.txt”);
citaj变量是否已声明?数组的大小是否大于行数?