【问题标题】:Destination file blank after writing with StreamWriter C#使用 StreamWriter C# 写入后目标文件为空
【发布时间】: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


【解决方案1】:

我以前也遇到过类似的问题。如果我没记错的话,解决方案归结为将 StreamWriter 包装在 using 块中,而不是创建它然后尝试关闭它,例如

    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();

        }

    }

【讨论】:

  • 如果代码无异常运行,那么添加using 语句将无济于事。这仍然是一个好主意,但它不能解释为什么他们的 OP 看到的是一个空白文件,IMO。
【解决方案2】:

对所有实现IDisposable 的东西都使用using 状态,例如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)

【讨论】:

    【解决方案3】:

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

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

    不清楚你是如何运行它的,但是如果它被配置为一个控制台应用程序并且你从一个控制台窗口运行它,那么你肯定会看到任何异常,我怀疑你会看到同样的东西。

    尝试将其更改为写入您绝对可以访问的位置 - 并尝试找出您之前没有看到异常的原因。

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

    【讨论】:

    • 谢谢,请看我上面的回复:)
    猜你喜欢
    • 2018-08-24
    • 2018-04-18
    • 2013-04-03
    • 1970-01-01
    • 2011-10-29
    • 2014-02-22
    • 2016-05-03
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多