显示随机打开的文本文件中的行

时间:2012-02-23 02:20:37

标签: c#

我似乎无法弄清楚什么是错的。我想在按下'Next'按钮时从随机的.txt文件中读取,然后在索引3处显示数组allLines []中包含的字符串。它似乎只打印该文件的索引而不是随机选择另一个要读取的文件。这只是一个帮助我学习使用闪存卡的小程序。我想可能有一个我可以下载,但任何想法?

namespace MSU_Flash_Cards
{
public partial class Form1 : Form
{
    DirectoryInfo di = new DirectoryInfo("C:\\Users\\Public\\cards\\");
    Random rand = new Random ();

    int cardside;
    int filecount;
    int fileindex;
    string[] filename;

    string[] allLines;
    // allLines[0] is for Card Name
    // allLines[1] is for Card Description
    // allLines[2] is for Card Front
    // allLines[3] is for Card Back       

    public Form1()
    {
        InitializeComponent();
        cardside = 0;
    }
    private void btnNewCardSave_Click(object sender, EventArgs e)
    {
        int x = 0;
        //store the text in the text boxes in an array
        string[] s_temp = new String[4];
        s_temp[0] = txtboxNewCardName.Text.ToString();
        s_temp[1] = txtboxNewCardDesc.Text.ToString();
        s_temp[2] = txtboxNewCardFront.Text.ToString();
        s_temp[3] = txtboxNewCardBack.Text.ToString();
        StreamWriter sw = new StreamWriter(string.Format("C:\\Users\\Public\\cards\\{0}.txt", s_temp[0])); // s_temp[0] is used here to define the file name to use.

        while (x <= 3)
        {
            //write each segment of the array to a file, lines from 0-3
            sw.WriteLine(s_temp[x].ToString());
            x++;
        }
        sw.Close();
    }
    private void btnNext_Click(object sender, EventArgs e)
    {    
        // Randomly get the next card
        SetNextCard();
        // Display the next cards 'back'
        txtboxCard.Text = string.Format("{0}", allLines[3]);
    }
    private void btnFlip_Click(object sender, EventArgs e)
    {
        if (cardside == 0)
        {
            // if the front is showing, switch to the back
            txtboxCard.Text = string.Format("{0}", allLines[3]);
            cardside = 1;
        }
        else
        {
            // if the back is listed, switch to the front
            txtboxCard.Text = string.Format("{0}", allLines[2]);
            cardside = 0;
        }
    }       
    private void SetNextCard()
    {   
        int x = 0;

        // Check the directory for files & count them
        FileInfo[] rgFiles = di.GetFiles("*.*");
        filecount = di.GetFiles().Length;

        // Create a new array based on the filecount
        filename = new String[filecount];

        // Save each file name in the array
        foreach (FileInfo fi in rgFiles)
        {
            filename[x] = fi.FullName;
            x++;
        }

        // Select randomly a file for reading
        fileindex = rand.Next(0, filecount); 

        // Read each line of the file and assign to a global array for use later
        allLines = File.ReadAllLines(string.Format("{0}", filename));

        }
    }
}

3 个答案:

答案 0 :(得分:1)

您设置fileindex的值,但从不使用它。您可能希望将最后一行更改为:

allLines = File.ReadAllLines(filename[fileindex]);

此外,fileindex应该是一个字段而不是一个局部变量似乎没有任何理由。

答案 1 :(得分:1)

您没有索引到您的文件名数组,索引丢失。

allLines = File.ReadAllLines(filename[fileindex]);

答案 2 :(得分:0)

当您尝试读取文件时,您没有索引到您的文件名数组,因此无法找到您要打开的文件。替换你的最后一行

allLines = File.ReadAllLines(string.Format("{0}", filename));

allLines = File.ReadAllLines(filename[fileindex]);

读取所选文件。