1

I'm trying to read and write from a txt file using C#. At the moment, I'm writing a program that reads the names in a list, shows them to the user, asks for another name, and then adds that one to the list. The reading is fine, but there's some problems with the writing.

The code compiles fine using CSC, and it executes fine, but after I type the name to add and hit enter, I get a window popping up saying

FileIO.exe has encountered a problem and needs to close.

Any idea what the problem is?

using System;
using System.IO;

public class Hello1
{
    public static void Main()
    {   
        int lines = File.ReadAllLines("Name.txt").Length;
        string[] stringArray = new string[lines + 1];
        StreamReader reader = new StreamReader("Name.txt");
        for(int i = 1;i <=lines;i++){
            stringArray[i-1] = reader.ReadLine();
        }
        for (int i = 1;i <=stringArray.Length;i++){
            Console.WriteLine(stringArray[i-1]);
        }
        Console.WriteLine("Please enter a name to add to the list.");
        stringArray[lines] = Console.ReadLine();
        using (System.IO.StreamWriter writer = new System.IO.StreamWriter("Name.txt", true)){
            writer.WriteLine(stringArray[lines]);
        }
    }
}
4

5 回答 5

2

您收到异常是因为您没有关闭您的reader, 只是reader.Close();在将文件读取到 Array 后放置。

更好的是使用using语句,因为StreamReader使用IDisposable接口,这将确保流的关闭以及它的处置。

string[] stringArray = new string[lines + 1];
using (StreamReader reader = new StreamReader("Name.txt"))
{
    for (int i = 1; i <= lines; i++)
    {
        stringArray[i - 1] = reader.ReadLine();
    }
}

只是一个旁注

File.ReadAllLines只是为了得到Length???,你可以像这样填充你的数组:

string[] stringArray = File.ReadAllLines("Name.txt");

而不是通过StreamReader.

于 2013-10-08T20:24:04.473 回答
2

我们稍微简化一下如何:

foreach (var line in File.ReadLines("Name.txt"))
{
    Console.WriteLine(line);
}
Console.WriteLine("Please enter a name to add to the list.");
var name = Console.ReadLine();
File.AppendLine("Name.txt", name):

现在您根本不需要处理 IO,因为您通过完全利用这些静态方法将其留给框架。

于 2013-10-08T20:24:48.083 回答
1

最好确保您了解控制台应用程序顶层的任何异常:

public class Hello1
{
    public static void Main()
    {
        try
        {
            // whatever
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception!);
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            Console.Write("Press ENTER to exit: ");
            Console.ReadLine();
        }
    }
}

这样,您就会知道为什么应用程序必须关闭。

此外,你需要让你StreamReader在一个using街区。

于 2013-10-08T20:34:09.283 回答
0

如果要将文件中的所有行读入数组,可以简单地使用:

string[] lines = File.ReadAllLines("Name.txt");

并使用该数组。

于 2013-10-08T20:22:53.557 回答
-2

像这样使用 reader.ReadToEnd 函数,完成后不要忘记关闭阅读器。:

StreamReader reader = new StreamReader("Name.txt");
string content = reader.ReadToEnd();
reader.Close();

你得到这个异常的原因是你没有在阅读后关闭阅读器。所以你不能在没有先调用 Close(); 的情况下写入它。方法。

您也可以使用 using 语句而不是像这样关闭它:

using (StreamReader reader = new StreamReader("Name.txt")){
    string content = reader.ReadToEnd();
};
于 2013-10-08T20:21:07.287 回答