C# 显示随机打开的文本文件中的行
我似乎不知道出了什么问题。我想在按下“下一步”按钮时读取随机的.txt文件,然后在索引3处显示数组allLines[]中包含的字符串。它似乎只打印该文件的索引,而不是随机选择另一个文件进行读取。这只是一个帮助我学习的小程序,使用闪存卡。我想可能有一个我可以下载,但有什么想法吗C# 显示随机打开的文本文件中的行,c#,C#,我似乎不知道出了什么问题。我想在按下“下一步”按钮时读取随机的.txt文件,然后在索引3处显示数组allLines[]中包含的字符串。它似乎只打印该文件的索引,而不是随机选择另一个文件进行读取。这只是一个帮助我学习的小程序,使用闪存卡。我想可能有一个我可以下载,但有什么想法吗 namespace MSU_Flash_Cards { public partial class Form1 : Form { DirectoryInfo di = new DirectoryInfo("C:\\U
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));
}
}
}
名称空间MSU\U闪存卡
{
公共部分类Form1:Form
{
DirectoryInfo di=newdirectoryinfo(“C:\\Users\\Public\\cards\\”;
Random rand=新的Random();
内卡德赛德;
int文件计数;
int文件索引;
字符串[]文件名;
字符串[]所有行;
//allLines[0]表示卡名
//所有行[1]用于卡片描述
//所有行[2]用于卡正面
//所有行[3]用于回卡
公共表格1()
{
初始化组件();
cardside=0;
}
私有void btnNewCardSave_单击(对象发送者,事件参数e)
{
int x=0;
//将文本存储在数组中的文本框中
字符串[]s_temp=新字符串[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]定义要使用的文件名。
while(x您设置了fileindex
的值,但从未使用它。您可能希望将最后一行更改为:
allLines = File.ReadAllLines(filename[fileindex]);
此外,似乎没有任何理由说明fileindex
应该是字段而不是局部变量。如果没有索引到文件名数组中,则缺少索引
allLines = File.ReadAllLines(filename[fileindex]);
当您尝试读取文件时,您没有在文件名数组中建立索引,因此它找不到要打开的文件。请替换最后一行
allLines = File.ReadAllLines(string.Format("{0}", filename));
与
阅读所选文件。您是否尝试过在调试器中浏览代码?我尝试过。int filecount正确表示文件夹中的文件数量,但文件名没有更改。可能是我太累了,遗漏了一些明显的内容。不过,我在这方面还是很新的。注意:使用string.Format
wa没有任何意义你经常使用它。txtboxCard.Text=string.Format(“{0}”,allLines[3]);
也可以简单地编写txtboxCard.Text=allLines[3];
你说得对。我把它放在那里是因为我原来用'\n'换行符格式化了它,但忘了把它取出来。谢谢。比如:allLines[index number]=File.ReadAllLines(filename[fileindex]);但随后添加一个foreach语句来添加文件中找到的每一行?他实际上是在向ReadAllLines()
传递一个string.Format()
字符串,因为这是string.Format()
返回的内容。但该字符串没有任何意义。
allLines = File.ReadAllLines(filename[fileindex]);