使用StreamWriter C#写入后目标文件为空

时间:2013-10-29 16:19:04

标签: c# streamwriter

我已经编写了一个控制台应用程序,它本身就像我希望的那样工作。它的主要输出在控制台中运行良好。但是我希望将循环内部的结果写入文本文件。我已经使用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();

        }
    }
}

3 个答案:

答案 0 :(得分:2)

我之前遇到过类似的问题。如果我没记错的话,解决方案是将StreamWriter包装在一个使用块中,而不是创建它然后尝试关闭它,例如。

    static void Main(string[] args)
    {
        using (var 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();

        }

    }

答案 1 :(得分:2)

using statament用于实现IDisposable的所有内容,例如StreamWriter。这将确保处置非托管资源(即使出错)。如果流AutoFlush propertyfalse(默认值),它也会刷新流。

using(var file = new System.IO.StreamWriter("c:\\Test.txt"))
{
    // ...
    file.WriteLine(x + ", " + y*10);
    // rest of code here ...
}

Does Stream.Dispose always call Stream.Close (and Stream.Flush)

答案 2 :(得分:2)

当我运行该代码时,我看到一个异常:

Unhandled Exception: System.UnauthorizedAccessException:
Access to the path 'c:\Test.txt' is denied.

目前尚不清楚你是如何运行它的,但是如果它被配置为控制台应用程序并且你从控制台窗口运行它所以你肯定会看到任何异常,我怀疑你会看到同样的事情。

尝试将其更改为写入您可以访问的某个地方 - 并尝试找出之前没有看到异常的原因。

此外,如其他答案所示,使用using语句肯定会更好 - 但如果没有创建文件,这将无法解释干净运行(无例外)。

相关问题