需要将TXT中的若干行值读取到数组中,然后随机抽取string [] = ?

解决方案 »

  1.   

    System.IO.TextReader tr = System.IO.File.OpenText("d:\\test123.txt");
    System.Collections.ArrayList al = new ArrayList();string tmp = tr.ReadLine();
    while(tmp != null)
    {
    al.Add(tmp);
    tmp = tr.ReadLine();
    }tr.Close();string[] txts = new string[al.Count];
    al.CopyTo(txts,0);
      

  2.   

    //然后是随机抽取
    System.Random rd = new Random();
    System.Collections.ArrayList alc = al.Clone();//随机抽取5个不重复的行
    for(int i=0;i<5;i++)
    {
    int len = alc.Count-1;
    int x = rd.Next(len);
    //输出alc[x]
    alc.RemoveAt(x);
    }
      

  3.   

    private void ReadTxt()
            {
                string filePath = Application.StartupPath + "\\test.txt";
                string[] fileStr = new string[100];
                int i = 0;
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
                StreamReader sr = new StreamReader(fs,System.Text.Encoding.Default );
                //sr.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = sr.ReadLine();
                while (strLine != null)
                {
                    this.textBox1.Text += strLine + "\r\n";
                    strLine = sr.ReadLine();
                    fileStr[i] = strLine;
                    i++;
                }
                sr.Close();
                fs.Close();        }