使用文件读取将整数添加到列表或数组中

Gig*_*tex 0 c# file-handling

我有一个带有一些数字的.txt文件

File:
1
2
3
4
Run Code Online (Sandbox Code Playgroud)

我想有一个方法来读取这些数字并将它们添加到列表或数组中,它将在消息框中显示它.

我现在有这个:

public void LaadVrijeKamers()
        {
            int KamerNummers = Convert.ToInt32(File.ReadAllText(@"x\Vrijekamers.txt"));
            MessageBox.Show(Convert.ToString(KamerNummers));
        }
Run Code Online (Sandbox Code Playgroud)

我在荷兰语中收到错误,说明如下:

Can not read the characters
Run Code Online (Sandbox Code Playgroud)

我认为File.ReadAllText仅适用于Strings,但我不确定.也许我转错了.

abd*_*dul 5

尝试逐行阅读并将字符串转换为整数:

var numbers = File.ReadLines(@"C:\path\numbers.txt").Select(int.Parse).ToList();
Run Code Online (Sandbox Code Playgroud)