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

时间:2015-04-30 21:17:27

标签: c# arrays streamreader streamwriter file.readalllines

我需要一些文件流的帮助。我有一个程序,将名称记录到将写入.txt文件的字符串数组中。然后,如果用户想要从.txt文件中的数组中删除特定字符串。程序将在.txt文件中搜索数组中的字符串,复制.txt文件中除匹配字符串外的所有行。然后,将所有行粘贴到temp .txt文件中,除了1个匹配的字符串。然后将temp .txt文件中的所有字符串行复制并粘贴回原始.txt文件,然后删除临时文件。 我遇到的问题是,我无法弄清楚如何将所有字符串行复制到temp .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]);

                }

我添加了很多评论,这样每个人都可以对我的思维过程有所了解。

0 个答案:

没有答案