我有点卡住了。我必须使用getItems
方法并将其输出到PrintItems.txt,但我不确定如何解决此问题。输出到.txt文件
这是我GetItems
方法:
public string getItems()
{
string strout = "stock items " + "\n";
foreach (Stock s in stock)
{
strout = strout + s.ToString() + "\n";
}
return strout;
}
这是我PrintItems
方法:
string filename = "printitems.txt";
int count = 0;
using (StreamWriter sw = new StreamWriter(filename))
{
while (count < 5 && count < stock.Count)
{
Stock t = stock[count];
sw.WriteLine(t);
count++;
}
}
它不工作,因为它不写入一个文件都没有。
错误吗?你是否确实没有写入文件 - IE或许“printitems.txt”没有出现在你期望的文件夹中,因为你没有为它指定路径等。另外'getItems'不是由您的文件编写代码使用? –
你是否检查过文件的'/ bin'目录? –
你需要输出getItems()的返回值吗?那你为什么不叫这个函数呢? – Kyborek