Click here to Skip to main content
15,885,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I`m writing a Windows Form application.
A text file is storing some names with codes and an index(like: Chris;987;5).
That text file is used then for reading it.

Now, I have 2 textboxes( userTXT and codeTXT) and a button (removeBtn).
A user must write its username in userTXT and a code that is given by the same application when making an "account" in codeTXT. When the button is pressed, the program should read the text file (data.txt), remove the line that contains the matching username and code and update the a datagridview which has each username as a row header cell.

The problem:

I`ve tried many ways to delete that line in the txt file (including: making a second instance of the file, removing the line and deleting the original file; using While function, reading the file to a list and use remove or remove at), but no one of these works. After the button is pressed, the text file looks the same as before trying to delete the line. Tried debugging and no errors, tried breakpoints and I didn`t find anything significant.

Please help me; Thanks in Advance - CCB
Posted
Updated 18-Jun-15 16:40pm
v2
Comments
PIEBALDconsult 18-Jun-15 22:32pm    
That would be why God invented databases.
Why are you trying to do such a silly thing?
ChrisCreateBoss 18-Jun-15 22:37pm    
Because I can't manage them, sincerely I don't know how to use them. And I prefer text files since it is easier for me. If you can provide me a code sample on how to use them I would really appreciate it. :)

1 solution

Read the file line by line and write the result in a different file line by line, skipping the required lines. Use these two classes:
https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx[^].

For output file, use temporary file name: https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx[^].

When it is done, rename temporary file to the original file, if this is what you need. This is how: https://msdn.microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx[^].

Don't forget to close all streams, otherwise the files will remain inaccessible. The best way to do it is to call IDisposable.Dispose methods of the reader and the writer, and, in turn, the best way to do it is to use using statement (not to be mixed up with using directive):
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^].

This is a very universal approach which can work for the files of any size.

Now, this is light-weight approach for smaller files: read all lines at once to memory, process the lines to ignore unwanted lines, write all lines to the file:
https://msdn.microsoft.com/en-us/library/system.io.file.readalllines%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.file.writealllines%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
ChrisCreateBoss 18-Jun-15 22:41pm    
Tried that too. And had no success
Sergey Alexandrovich Kryukov 18-Jun-15 22:47pm    
No, you did not, otherwise it would work. If you need further help, show your code sample.
Try to minimize your code sample as much as your can, but keep it comprehensive. It should be just one file which should compile. If you do it, I'll find your bug, unless you fix it while doing it. Now, it's your turn. If the case is resolved, please accept the answer.

You should not "try". You should do this work in sure manner, understanding each line you are writing.

—SA

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