我需要从 C# 中的文本文件向用户 return 一行字符串
I need to return a line of string to the user from a text file in C#
public class QuoteGenerator
{
public static randomQuote()
{
string t = "Quotes.txt";
List<string> Quotes = new List<string>();
using (StreamReader quoteReader = new StreamReader(t))
{
string line = "";
while ((line = quoteReader.ReadLine()) != null)
{
Quotes.Add(line);
}
}
string[] response = Quotes.ToArray();
string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);
return (shuffle[0]);
}
}
这是有效的,我认为我上面的 StreamReader 代码也能正常工作:
public string randomQuote()
{
string[] response = new string[] {"The fortune you seek, is in another bot"
, "Someone has Googled you recently"
, "This fortune no good. Try another"
, "404 fortune not found"};
string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);
return shuffle[0];
}
我需要return引用 StreamReader 方法的第一行,为什么我放在一起的代码似乎不起作用?我考虑过对引号进行硬编码,但将它们保存在文本文件中也许是个好主意。我想我不明白使用 StreamReader 是如何工作的。谁能解释一下,我从 7 月才开始编码。谢谢!
假设您的 Quotes.txt 文件位于 bin 目录中,StreamReader 代码工作正常。唯一明显的是您没有为 randomQuote 方法指定 return 类型。
public static string randomQuote()
public class QuoteGenerator
{
public static randomQuote()
{
string t = "Quotes.txt";
List<string> Quotes = new List<string>();
using (StreamReader quoteReader = new StreamReader(t))
{
string line = "";
while ((line = quoteReader.ReadLine()) != null)
{
Quotes.Add(line);
}
}
string[] response = Quotes.ToArray();
string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);
return (shuffle[0]);
}
}
这是有效的,我认为我上面的 StreamReader 代码也能正常工作:
public string randomQuote()
{
string[] response = new string[] {"The fortune you seek, is in another bot"
, "Someone has Googled you recently"
, "This fortune no good. Try another"
, "404 fortune not found"};
string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);
return shuffle[0];
}
我需要return引用 StreamReader 方法的第一行,为什么我放在一起的代码似乎不起作用?我考虑过对引号进行硬编码,但将它们保存在文本文件中也许是个好主意。我想我不明白使用 StreamReader 是如何工作的。谁能解释一下,我从 7 月才开始编码。谢谢!
假设您的 Quotes.txt 文件位于 bin 目录中,StreamReader 代码工作正常。唯一明显的是您没有为 randomQuote 方法指定 return 类型。
public static string randomQuote()