我编写了一个控制台应用程序,它本身可以按我的意愿工作。它的主要输出在控制台中效果很好。但是我想将循环内的结果写入文本文件。我已经使用 StreamWriter 来尝试这个,虽然我在编译或运行时没有收到错误,但我的 C: 驱动器中的文件仍然是空白的。谁能发现我错过的任何愚蠢或快速的事情?
如果您能提供帮助,请提前感谢您。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2
{
class Program
{
static void Main(string[] args)
{
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\Test.txt");
double z = 0;
double x = 1;
double y = 1;
Console.WriteLine("How many lines should be written to the file?");
Console.WriteLine();
z = double.Parse(System.Console.ReadLine());
Console.WriteLine("Writing " + z + "lines to file!");
Console.WriteLine();
while (z > 0)
{
y = Math.Pow(x, 2);
Console.WriteLine(x + ", " + y*10);
file.WriteLine(x + ", " + y*10);
z = z - 1;
x = x + 1;
}
Console.WriteLine();
Console.WriteLine("**Generation Complete**");
Console.WriteLine();
Console.WriteLine("-------------------------------");
Console.WriteLine();
Console.WriteLine("**Writing to file successful**");
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
file.Close();
}
}
}