C# 将文本文件读入数组?

C# 将文本文件读入数组?,c#,visual-studio-2013,C#,Visual Studio 2013,我试图通过将联系人存储在文本文件中来制作联系人簿。例如,假设我有两个字符串,分别称为名字和姓氏,在文本文件中我有一个名字,在下一行有一个姓氏。这是我目前拥有的代码,但我不确定在do循环中需要做什么,如何读取一行,将其插入字符串xxx,读取下一行并将其存储在字符串yyy中 public Contacts[] getAllContacts() { List<Contacts> theContactList = new List<Contacts&g

我试图通过将联系人存储在文本文件中来制作联系人簿。例如,假设我有两个字符串,分别称为名字和姓氏,在文本文件中我有一个名字,在下一行有一个姓氏。这是我目前拥有的代码,但我不确定在do循环中需要做什么,如何读取一行,将其插入字符串xxx,读取下一行并将其存储在字符串yyy中

    public Contacts[] getAllContacts()
    {

        List<Contacts> theContactList = new List<Contacts>();

        string file_name = "Contacts.txt";
        string textLine = "";

        if (System.IO.File.Exists(file_name) == true)
        {
            System.IO.StreamReader objReader;
            objReader = new System.IO.StreamReader(file_name);


            do
            {
                objReader.ReadLine() + "\r\n";

            } while (objReader.Peek() != 1);

        }
        if (theContactList.Count > 0)
        {
            return theContactList.ToArray();

        }
        else
        {
            return null;
        }


    }
公共联系人[]getAllContacts()
{
列出ContactList=新列表();
字符串文件_name=“Contacts.txt”;
字符串textLine=“”;
if(System.IO.File.Exists(File_name)==true)
{
System.IO.StreamReader objReader;
objReader=new System.IO.StreamReader(文件名);
做
{
objReader.ReadLine()+“\r\n”;
}while(objReader.Peek()!=1);
}
如果(ContactList.Count>0)
{
返回contactlist.ToArray();
}
其他的
{
返回null;
}
}

我还需要能够在文本文件中存储多个联系人和更多字段,如地址、电话号码等。

假设始终有两行,并且它们的顺序相同,则以下代码可以在其中工作:

public Contacts[] getAllContacts()
{

    List<Contacts> theContactList = new List<Contacts>();

    string file_name = "Contacts.txt";
    string textLine = "";
    bool firstNameLine = true;

    if (System.IO.File.Exists(file_name) == true)
    {
        System.IO.StreamReader objReader;
        objReader = new System.IO.StreamReader(file_name);
        Contact newContact;

        do
        {
            textLine = objReader.ReadLine();
            if (firstNameLine)
            {
                newContact = new Contact();
                newContact.firstName = textLine;
            }
            else
            {
                newContact.sureName = textLine;
                theContactList.Add(newContact);
            }

            firstNameLine = !firstNameLine;

        } while (objReader.Peek() != 1);

    }
    if (theContactList.Count > 0)
    {
        return theContactList.ToArray();

    }
    else
    {
        return null;
    }


}
公共联系人[]getAllContacts()
{
列出ContactList=新列表();
字符串文件_name=“Contacts.txt”;
字符串textLine=“”;
bool firstNameLine=true;
if(System.IO.File.Exists(File_name)==true)
{
System.IO.StreamReader objReader;
objReader=new System.IO.StreamReader(文件名);
联系新联系人;
做
{
textLine=objReader.ReadLine();
if(第一个名称行)
{
newContact=新联系人();
newContact.firstName=文本行;
}
其他的
{
newContact.sureName=textLine;
联系人列表。添加(新联系人);
}
firstNameLine=!firstNameLine;
}while(objReader.Peek()!=1);
}
如果(ContactList.Count>0)
{
返回contactlist.ToArray();
}
其他的
{
返回null;
}
}

因此,在本例中,代码使用一个布尔变量,定义使用哪一行读取来填充哪一个字符串。如果超过2行(超过2个字符串),则需要一个整数变量(增加+1,然后在读取所需的最后一行时重置),而不是IF-a-switch语句。

在假定始终有2行并且它们的顺序相同的情况下,以下代码可以在那里工作:

public Contacts[] getAllContacts()
{

    List<Contacts> theContactList = new List<Contacts>();

    string file_name = "Contacts.txt";
    string textLine = "";
    bool firstNameLine = true;

    if (System.IO.File.Exists(file_name) == true)
    {
        System.IO.StreamReader objReader;
        objReader = new System.IO.StreamReader(file_name);
        Contact newContact;

        do
        {
            textLine = objReader.ReadLine();
            if (firstNameLine)
            {
                newContact = new Contact();
                newContact.firstName = textLine;
            }
            else
            {
                newContact.sureName = textLine;
                theContactList.Add(newContact);
            }

            firstNameLine = !firstNameLine;

        } while (objReader.Peek() != 1);

    }
    if (theContactList.Count > 0)
    {
        return theContactList.ToArray();

    }
    else
    {
        return null;
    }


}
公共联系人[]getAllContacts()
{
列出ContactList=新列表();
字符串文件_name=“Contacts.txt”;
字符串textLine=“”;
bool firstNameLine=true;
if(System.IO.File.Exists(File_name)==true)
{
System.IO.StreamReader objReader;
objReader=new System.IO.StreamReader(文件名);
联系新联系人;
做
{
textLine=objReader.ReadLine();
if(第一个名称行)
{
newContact=新联系人();
newContact.firstName=文本行;
}
其他的
{
newContact.sureName=textLine;
联系人列表。添加(新联系人);
}
firstNameLine=!firstNameLine;
}while(objReader.Peek()!=1);
}
如果(ContactList.Count>0)
{
返回contactlist.ToArray();
}
其他的
{
返回null;
}
}
因此,在本例中,代码使用一个布尔变量,定义使用哪一行读取来填充哪一个字符串。如果超过2行(超过2个字符串),则需要一个整数变量(增加+1,然后在读取所需的最后一行时重置),而不是IF开关语句。

使用以下命令:

string [] file_array = File.ReadAllLines("path");
PS:
使用System.IO
此函数用于读取整个文件并将其行存储在数组中。您可以编辑数组行,然后使用

File.writeAllines(“路径”,文件数组)

要将它们放回文件中,请使用以下命令:

string [] file_array = File.ReadAllLines("path");
PS:
使用System.IO
此函数用于读取整个文件并将其行存储在数组中。您可以编辑数组行,然后使用

File.writeAllines(“路径”,文件数组)


将它们放回文件中。

可能最好使用XML或CSV文件,甚至只是一个文本文件,但将每个“联系人”放在同一行并解析出来

下面的代码将执行您想要的操作

 public Contacts[] GetAllContacts()
    {
        List<Contacts> contacts = new List<Contacts>();
        const string filePath = "Contacts.txt";

        if (File.Exists(filePath))
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                do
                {
                    Contacts contact = new Contacts();
                    contact.FirstName = sr.ReadLine();
                    contact.Surname = sr.ReadLine();
                    contacts.Add(contact);

                } while (sr.Peek() != -1);
            }
        }

        return contacts.ToArray();
    }
公共联系人[]GetAllContacts()
{
列表联系人=新列表();
常量字符串filePath=“Contacts.txt”;
if(File.Exists(filePath))
{
使用(StreamReader sr=新的StreamReader(文件路径))
{
做
{
联系人=新联系人();
contact.FirstName=sr.ReadLine();
contact.lasname=sr.ReadLine();
联系人。添加(联系人);
}而(sr.Peek()!=-1);
}
}
返回contacts.ToArray();
}

可能最好使用XML或CSV文件,甚至只是一个文本文件,但每个“联系人”都在同一行并解析出来

下面的代码将执行您想要的操作

 public Contacts[] GetAllContacts()
    {
        List<Contacts> contacts = new List<Contacts>();
        const string filePath = "Contacts.txt";

        if (File.Exists(filePath))
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                do
                {
                    Contacts contact = new Contacts();
                    contact.FirstName = sr.ReadLine();
                    contact.Surname = sr.ReadLine();
                    contacts.Add(contact);

                } while (sr.Peek() != -1);
            }
        }

        return contacts.ToArray();
    }
公共联系人[]GetAllContacts()
{
列表联系人=新列表();
常量字符串filePath=“Contacts.txt”;
if(File.Exists(filePath))
{
使用(StreamReader sr=新的StreamReader(文件路径))
{
做
{
联系人=新联系人();
contact.FirstName=sr.ReadLine();