电话列表c#(从文件中读取)

电话列表c#(从文件中读取),c#,C#,这是一个非常简单的程序。这是一个家庭作业项目。程序应该启动,读取一个.txt文件,填充姓名列表,当你点击姓名时,手机号码就会显示出来。我有代码,没有错误,没有警告。它不会读取.txt文件,我不知道为什么。我一直在搜我的书,youtube,甚至在这里,但无法确定。任何帮助都将不胜感激。这是我目前的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usi

这是一个非常简单的程序。这是一个家庭作业项目。程序应该启动,读取一个.txt文件,填充姓名列表,当你点击姓名时,手机号码就会显示出来。我有代码,没有错误,没有警告。它不会读取.txt文件,我不知道为什么。我一直在搜我的书,youtube,甚至在这里,但无法确定。任何帮助都将不胜感激。这是我目前的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Phonebook
{
    struct PhoneBookEntry
    {
        public string name;
        public string phone;
    }
    public partial class Form1 : Form
    {
        // FIeld to hold a list of PhoneBookEntry objects.
       private List<PhoneBookEntry> phoneList = new List<PhoneBookEntry>();

        public Form1()
        {
            InitializeComponent();
       }

        // The ReadFile method reads the contents of the
        //PhoneList.txt file and tores it as PhoneBokeEntry
        // objects in the phoneList.
        private void ReadFile()
        {
            try
            {
                StreamReader inputFile;  // To read the file
                string line;             // To hold a line from the file

                // Create an instance of the PhoneBookEntry structure.
                PhoneBookEntry entry = new PhoneBookEntry();

                // Create a delimiter array.
                char[] delim = { ',' };

                // Open the PhoneList file.
                inputFile = File.OpenText("PhoneList.txt");

                // Read the lines from the file.
                while (!inputFile.EndOfStream)
                {

                    // Read a line from the file.
                    line = inputFile.ReadLine();

                    // Tokenize the line
                    string[] tokens = line.Split(delim);

                   // Store the tokens in the entry object.
                    entry.name = tokens[0];
                    entry.phone = tokens[1];

                // Add the entry object to the List.
                phoneList.Add(entry);
                }
            }
            catch (Exception ex)
            {
              // Display an error message.    
               MessageBox.Show(ex.Message);
            }
        }
        // The DisplayNames method displays the list of names
        // in the namesListBox conrol.
        private void DisplayNames()
        {
            foreach (PhoneBookEntry entry in phoneList)
        {
            nameListBox.Items.Add(entry.name);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Read the PhoneList.txt file.
            ReadFile();

            // DIsplay the names.
            DisplayNames();
        }

        private void nameListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the index of the selected item.
            int index = nameListBox.SelectedIndex;

            // Display the corresponding phone number.
            phoneLabel.Text = phoneList[index].phone;
        }

         private void exitButton_Click(object sender, EventArgs e)
        {
            // close the form.
            this.Close();
        }

    private void Form1_Load_1(object sender, EventArgs e)
    {

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
命名空间电话簿
{
结构电话簿条目
{
公共字符串名称;
公用串电话;
}
公共部分类Form1:Form
{
//字段以保存PhoneBookEntry对象列表。
私有列表电话列表=新列表();
公共表格1()
{
初始化组件();
}
//ReadFile方法读取
//PhoneList.txt文件,并将其存储为PhoneBokeEntry
//电话列表中的对象。
私有void ReadFile()
{
尝试
{
StreamReader inputFile;//读取文件
string line;//保存文件中的一行
//创建PhoneBookEntry结构的实例。
PhoneBookEntry=新建PhoneBookEntry();
//创建分隔符数组。
char[]delim={',};
//打开电话列表文件。
inputFile=File.OpenText(“PhoneList.txt”);
//读取文件中的行。
而(!inputFile.EndOfStream)
{
//从文件中读取一行。
line=inputFile.ReadLine();
//标记该行
string[]tokens=line.Split(delim);
//将令牌存储在entry对象中。
entry.name=令牌[0];
entry.phone=代币[1];
//将条目对象添加到列表中。
电话列表。添加(条目);
}
}
捕获(例外情况除外)
{
//显示错误消息。
MessageBox.Show(例如Message);
}
}
//DisplayNames方法显示名称列表
//在名称列表框中控制。
私有void DisplayNames()
{
foreach(电话列表中的电话簿条目)
{
nameListBox.Items.Add(entry.name);
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
//读取PhoneList.txt文件。
ReadFile();
//显示名称。
显示名称();
}
私有无效名称列表框\u SelectedIndexChanged(对象发送方,事件参数e)
{
//获取所选项目的索引。
int index=nameListBox.SelectedIndex;
//显示相应的电话号码。
phoneLabel.Text=电话列表[索引]。电话;
}
私有无效退出按钮单击(对象发送者,事件参数e)
{
//关闭窗体。
这个。关闭();
}
私有void Form1_Load_1(对象发送方,事件参数e)
{
}
}

}

文件是否在正确的文件夹中?我在bin文件夹中。它必须在运行程序的Debug文件夹中,并且在重新显示其所在的名称之前,您需要清除列表框项目。bin/debug-its使用.exeremove ReadFile()函数中的try块,它可能会引发一个异常,您可能因此而丢失了该异常