将文本文件中数组中的字符串删除、复制和粘贴到文本文件中的新数组

Delete, copy, and paste string in an array in a text file to a new arry in a text file

我需要一些关于文件流的帮助。我有一个程序可以将名称记录到将写入 .txt 文件的字符串数组中。然后如果用户想从 .txt 文件中的数组中删除特定的字符串。程序会在.txt文件中搜索数组中的字符串,复制.txt文件中除匹配字符串外的所有行。然后,将除第 1 个匹配字符串之外的所有行粘贴到临时 .txt 文件中。然后将临时 .txt 文件中的所有字符串行复制并粘贴回原始 .txt 文件,然后删除临时文件。 我遇到的问题是,我不知道如何将所有字符串行复制到临时 .txt 文件。我知道它们没有被复制,因为我使用列表框(出于诊断原因)输出临时文件中的所有内容,但没有显示任何内容。整个过程我连F8都写的好像是写的,其实不是。在这部分解决之前,我什至无法开始考虑其余的编程,我需要知道为什么它不写入临时文件。您提供的任何帮助都会很有帮助。再次感谢。

        string[] names = File.ReadAllLines("name.txt");
        string fullName, firstName, lastName;
        fullName = tbInput.Text.ToUpper();
        double Fullname;
        int space;
        int fullNameLength;

        if (fullName.Contains(" "))
        {
            //This is for the Last name//
            space = fullName.IndexOf(" ");
            fullNameLength = fullName.Length;
            space++;
            lastName = fullName.Substring(space, fullNameLength - space);
            //This is for the first name//
            firstName = fullName.Substring(0, space);
            fullName = lastName + "," + firstName;
            //If the input name is valid, then it will procceed to the next if statment//
            Array.Sort(names);
            //if the fullname matches a string in the array, get the position. If no match is found then name was never recorded//
            int Position = Array.IndexOf(names, fullName);
            //Since arrays start at 0, if a position is found, the position WILL be greater than -1, so run the if statment//.
            if (Position > -1)
            {

                //Please ingnore these 2 lines, these are a work in progress for when i can move onto the next step of deletion and what not.//
               // FileStream name = new FileStream("name.txt", FileMode.Open, FileAccess.Write);
                //StreamWriter write = new StreamWriter(name);

               //I want to Open the Temps.txt file and seek the last line in the file to indicate where it need to write the next string//
                //while looping through the array "names"//

                for (int i = 0; i < names.Length; i++)
                {
                    //While looping through the array, "i" will increase by 1 each time the loop runs, when "i" equals the position(or index)//
                    //skip it. For everything else, read the line of the current index in the arry and write it to the temp.txt file// 
                    if (i == Position)
                    {
                        names.Skip(Position);
                    }
                    else
                    {
                        FileStream sw = new FileStream("Temp.txt", FileMode.Append, FileAccess.Write);
                        StreamWriter write2 = new StreamWriter(sw);
                        string input = names[i];
                        write2.WriteLine(input);
                        sw.Close();
                    }
                 }
                 //This part is used to loop through the temp file and output to a temp listbox to see if data is actually writing//
                string[] Temp = File.ReadAllLines("Temp.txt");

                for (int j = 0; j < Temp.Length; j++)
                {
                    lbtest.Items.Add(Temp[j]);

                }

我添加了很多评论,以便每个人都可以了解我的思维过程。

为什么要麻烦临时文件?由于您正在执行 File.ReadAllLines() 存储,因此结果为 List<string>。执行 IndexOf() 检查是否在列表中,如果它大于 -1 则将其删除(您正在使用 Position 变量执行此操作)。然后使用 File.WriteAllLines() 并给它你的列表。

List<string> names = new List<string>(File.ReadAllLines("name.txt"));
string fullName = tbInput.Text.ToUpper();

int fullNameIndex = names.IndexOf(fullName);
if (fullNameIndex > -1)
{
    names.RemoveAt(fullNameIndex);
}

File.WriteAllLines("name.txt", names);