开发者

Attemping to Remove Lines from Text File

I have created a text file that stores different flight information. I开发者_如何转开发 want the admins to be able to remove flights from this page by clicking a 'remove' button associated. I have the following code:

    private void removeButtons()
    {
        for (int x = 0; x < buttons.Count(); x++)
        {
            Button booked = buttons[x];
            booked.Click += new EventHandler(removeFlights);
        }
    }

    private void removeFlights(object sender, EventArgs e)
    {
        string tempFile = Path.GetTempFileName();
        using (var sr = new StreamReader(AppendTexts.flightPathText))//Location of file
        {
            using (var sw = new StreamWriter(tempFile))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    String[] flights = line.Split(',');
                    for (int x = 0; x < buttons.Count(); x++)//If the button clicked matches all the fields, skip it, otherwise write it to temp file
                    {
                        if (airline[0].Text == flights[0] & price[x].Text == flights[1] & cityStartMatch[x].Text == flights[2] & stateStartMatch[x].Text == flights[3] &
                            cityDestMatch[x].Text == flights[4] & stateDestMatch[x].Text == flights[5] & seatsMatch[x].Text == flights[6] & dateMatch[x].Text == flights[7])
                        {

                        }
                        else
                        {
                            sw.WriteLine(flights[0] + "," + flights[1] + "," + flights[2] + "," + flights[3] + "," + flights[4] + "," + flights[5] + "," + flights[6] + "," +  flights[7]);
                        }
                    }
                }
                sr.Close();
            }
        }
        File.Delete(AppendTexts.flightPathText);
        File.Move(tempFile, AppendTexts.flightPathText);
    }

The problem I am having, is that instead of removing the old lines, it just re-adds the other ones to the file. So if I have a flight using the Delta airline and another using Southwest, and I attempt to remove the Delta airline, when I list the flights again, the Delta airline shows up once, but the Southwest airline shows up twice.


A. Using a text file as a database in a bad practice.

B. When deleting multiple values from a collection, either use a temporary collection that you write good value to. Then delete original collection and overwrite with temp one. Or... Delete in-place from end to beginning, so that current index is not messed up by deleted. (Use of temp file is more recommended when deleting from large text files, in the second method you also have to keep reducing file size.)

C. In your solution, you didn't change position to correct one, calculsted relative to beginning of file, before each write (and readjust before each read).


Databases are great, really useful for this sort of application, and will greatly improve your applications.

I'm a developer by trade, and spend about 20 / 30% of my time writing SQL (database language), and really this is what you should be using for a ASP.NET app or any web app for that matter.

Once you learn it and start using it fully, you'll wonder how you ever did without it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜