C# 从txt文件读写

C# 从txt文件读写,c#,file-io,C#,File Io,我正在尝试使用C#从txt文件读写。目前,我正在编写一个程序,读取列表中的名称,将其显示给用户,请求另一个名称,然后将该名称添加到列表中。阅读很好,但写作上有一些问题 代码使用CSC编译得很好,执行得很好,但在我键入要添加的名称并点击enter键后,会弹出一个窗口,显示 FileIO.exe遇到问题,需要关闭 知道问题出在哪里吗 using System; using System.IO; public class Hello1 { public static void Main()

我正在尝试使用C#从txt文件读写。目前,我正在编写一个程序,读取列表中的名称,将其显示给用户,请求另一个名称,然后将该名称添加到列表中。阅读很好,但写作上有一些问题

代码使用CSC编译得很好,执行得很好,但在我键入要添加的名称并点击enter键后,会弹出一个窗口,显示

FileIO.exe遇到问题,需要关闭

知道问题出在哪里吗

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]);
        }
    }
}
使用系统;
使用System.IO;
公共类Hello1
{
公共静态void Main()
{   
int lines=File.ReadAllLines(“Name.txt”).Length;
string[]stringArray=新字符串[行+1];
StreamReader=新的StreamReader(“Name.txt”);

对于(int i=1;i请像这样使用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();
};

如果要将文件中的所有行读取到数组中,只需使用:

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

并使用该数组。

由于没有关闭
读卡器,所以出现异常,只需在将文件读取到数组后放置
读卡器.Close();

更好的方法是使用
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();
    }
}

与其通过
StreamReader

不如简化一下:

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,因为您完全利用这些静态方法,将IO留给框架处理。

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

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
块中。

对读取器也使用
using
语句。
using(var reader=new StreamReader(“Name.txt”){…}
在重新打开流进行写入之前关闭流。最好使用“using”语句为什么要使用
文件。ReadAllLines
,然后只计算长度,然后重新读取所有内容?实现这一点的方法要简单得多…你应该使用
using
语句-我认为你没有抓住问题的重点,而h是逐行读取文件。不关闭读卡器不会导致异常,你只是泄漏了一个资源。谢谢Jon,也添加了这个!Servy他正在试图在文件仍然打开时写入它。@Dimo:你仍然在使用
ReadToEnd
,这与OP想要的相反-而且你没有任何理由有一个备用分号。为什么要费心阅读呢正在初始化文件?之后你将忽略字符串数组…@Jon,你能不能用file.ReadAllText来编写整个文件?啊,我没有注意到它正在将其转储到控制台-抱歉。请注意,你的代码在上一次迭代中将失败-你想要
@JonSkeet,谢谢你的错误。它最初是
1
为基础,我试图使其
0
为基础。是的,你说得对,让我们使用
阅读行上的
foreach
-让我编辑。
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();
        }
    }
}