1

我需要一些文件流方面的帮助。我有一个程序,它将名称记录到一个字符串数组中,该数组将被写入一个 .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]);

                }

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

4

1 回答 1

0

为什么要打扰临时文件?因为你正在做一个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);
于 2015-04-30T21:31:27.193 回答