【发布时间】:2016-02-28 13:57:17
【问题描述】:
我不明白为什么输出文件不包含任何文本? 为什么数组不写入文件?运行时我没有收到任何错误..
Console.Write("Please enter a name for the homework: ");
hmwk[1].name = Console.ReadLine();
Console.Write("Please enter the subject: ");
hmwk[1].type = Console.ReadLine();
redo:
Console.Write("Please enter the date the homework was set xx/xx/xx: ");
string toconvert = Console.ReadLine();
DateTime temp;
if (DateTime.TryParse(toconvert, out temp)== false)
{
goto redo;
}
DateTime converted = DateTime.Parse(toconvert);
hmwk[1].dateset = converted;
redo2:
Console.Write("Please enter the date the homework deadline is: ");
toconvert = Console.ReadLine();
if (DateTime.TryParse(toconvert, out temp) == false)
{
goto redo2;
}
converted = DateTime.Parse(toconvert);
hmwk[1].deadline = converted;
StreamWriter outfile = new StreamWriter("homework.txt", false);
outfile.WriteLine(hmwk[1].name);
outfile.WriteLine(hmwk[1].type);
outfile.WriteLine(hmwk[1].dateset);
outfile.WriteLine(hmwk[1].deadline);
Console.WriteLine("You have added the homework. Press Enter.");
【问题讨论】:
-
Linked duplicate 显示了写入文件的正确方法以及指向 MSDN 上更多信息的链接。正如 Ksv3n 指出的那样,您缺少
using。另请查看stackoverflow.com/questions/18671774/…,因为使用goto通常不受欢迎。 -
谢谢,我去看看那个链接,我听说这是“不好的做法”,但我不知道为什么。
标签: c#