【发布时间】:2013-10-29 16:19:03
【问题描述】:
我编写了一个控制台应用程序,它本身可以按我的意愿工作。它的主要输出在控制台中效果很好。但是我想将循环内的结果写入文本文件。我已经使用 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();
}
}
}
【问题讨论】:
-
@DaveDev、Jon Skeet 等。感谢您的回复。在这个时间点,我确信这不是权限问题,因为正在创建文件。但是当我打开时,它是空白的。这包括使用“使用”语句。有任何想法吗? 忽略。现在工作正常。非常感谢大家
-
知道出了什么问题吗?
标签: c# streamwriter