Click here to Skip to main content
16,013,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file which looks contain such data:

Item name
Price
Quantity
Item name
Price
Quantity
Item name
Price
Quantity
...

Then I'm getting this data to listview with such code:

C#
listView1.Columns.Add("Item name / title", 400);
listView1.Columns.Add("Selling price", 70);
listView1.Columns.Add("Quantity / In stock", 100);


using (StreamReader sr = new StreamReader(@"test.txt"))
{
    while (-1 < sr.Peek())
    {
        try
        {
            string name = sr.ReadLine();
            string email = sr.ReadLine();
            string quantity = sr.ReadLine();
            var lvi = new ListViewItem(name);
            lvi.SubItems.Add(email);
            lvi.SubItems.Add(quantity);
            listView1.Items.Add(lvi);
        }
        catch (Exception) { }
    }
    sr.Close();
}


Now I'm implementing removing item function, I want to do it like this:
- Add button, on which click selected item is deleted from text file (by finding a line which contains item name, removing it and removing next two lines after it too). And then refresh list either by removing single items or clearing and reloading list.

My struggle now is - how to find and remove the lines in text file?
Posted

1 solution

This is one of the reasons why usign text files for this kind of thing is not common: it's a PITA.

Text files do not support records of any form, so you can't say "delete this line" because a text file does not really know about lines - it knows about newline characters, byt that is not the same thing at all.

In order to delete a line from a text file, you must:
1) Read the text file up to the point of the line you wish to delete, writing the data out to a new file at the same time.
2) Read past the line you want rid off, no writing it to the output file
3) Read the rest of the file, writing it to the output file.
4) Close both files.
5) Delete the original file.
6) Rename the output file to the original file name.

The process is much the same for inserting lines.

If you want to do this on a regular basis, or even have more than one user looking at the file, then I would strongly suggest you move to a more structured storage solution, such as a small database.
 
Share this answer
 
Comments
Tautvydas Deržinskas 5-May-13 4:30am    
Inserting new lines, could be just by writing new lines to the original text file (because there wouldn't be a need to put them in certain positions).

I am sure there is other way of doing it. Even like doing managing of listview items without touching test.txt file and after, write contents of listview to database file replacing old values... But I am also almost sure it's posible just find a selected value and delete the line.
OriginalGriff 5-May-13 4:40am    
That's not inserting, that only works for appends at the end (or you overwrite existing content)
If you treat your text file as structured storage by "recordising" it and making each line the same length, then you can do "inserts" and "deletes" by marking records as deleted in the file and re-using the delete marked records when you insert a new one. It's a lot of work though, and it is a lot simpler not to reinvent the wheel and use a system which handles that for you - SQLite or SqlCE for example. You also get a shed load of extra benefits like filtering, searching and so forth built in for you. Or there ate XMLDocuments where you can do the same thing (but they really use text files, and handle the crap for you by doing what I said above in the background)
Tautvydas Deržinskas 5-May-13 5:36am    
Thank you for the info, I will try now with SQLite. I wonder how it works after building application, is the database and all functionality inside it, or user of the app will have to install some kind of SQLite addition to his OS?
OriginalGriff 5-May-13 5:56am    
The database file remains outside your application EXE file (in the same way your text file would have), but SQLite just needs the DLL files that your application references included in the setup and deployment. It's not like Sql Server or MySql which require a dedicated server as it's single user only.

BTW: When it comes to picking a place to put your file (DB or Text) don't try to put it in the application folder - that can cause all sorts of permission problems with released software. There is a tip here which suggests better places:
http://www.codeproject.com/Tips/370232/Where-should-I-store-my-data

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900