vb.net - display names from text

XK8ER
XK8ER used Ask the Experts™
on
hello,
I have a text file with first names each one line by line..
I would like to know how can i display the first 10 records and delete them.

        Dim x As Integer
        For x = 1 To 10
                Call Get_Names(x)
        Next x

right now I have two functions one that read the first line and one that deletes the first one..
but doing this it has to open the file 10 times to read an another 10 times for deletes..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
For a = 1 To 10
call Get_Names(x)
 
 Next


2. another option

Do While x<11
call Get_Names(x)
Loop
why not read all the lines, perform our action and then delete the file completely:-
var lines = File.ReadAllLines("FilePath");

            var loopCounter = lines.Count()/10 + (lines.Count()%10 > 0 ? 1 : 0);

            for(int i=0;i<loopCounter;i++)
            {
                lines.Take(10).ToList().ForEach(Console.WriteLine);
            }

            File.Delete("FilePath");

Open in new window

Author

Commented:
no i dont want to read the whole file just first 10 lines then remove them
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

var filePath = string.Concat(Directory.GetCurrentDirectory(), @"\Test.txt");
                var allLines = File.ReadAllLines(filePath);

                var counter = allLines.Count()/10 + (allLines.Count()%10 > 0 ? 1 : 0);
                const int itemToTake = 10;

                using(var writer = new StreamWriter(filePath,false))
                {
                    for (int i = 0, j = 0; i < counter; i++, j = j + itemToTake)
                    {
                        var first10Lines = allLines.Skip(j).Take(itemToTake);

                        var otherLines = allLines.Skip(j + itemToTake);

                        var sb = new StringBuilder();
                        otherLines.ToList().ForEach(s =>
                                                        {
                                                            sb.Append(s);
                                                            sb.Append(Environment.NewLine);
                                                        });

                        writer.Write(sb);
                    }
                }

Open in new window

Try this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FILE_NAME As String = "C:\TEST.txt"
        Dim x As Integer
        For x = 1 To 10
            DeleteLine(FILE_NAME, 0)
        Next
    End Sub

    Public Sub DeleteLine(ByRef FileAddress As String, ByRef line As Integer)
        Dim TheFileLines As New List(Of String)
        TheFileLines.AddRange(System.IO.File.ReadAllLines(FileAddress))
        TheFileLines.RemoveAt(line)
        System.IO.File.WriteAllLines(FileAddress, TheFileLines.ToArray)
    End Sub
If you do not want to read all the lines, easily achieved by amending this line to just read 10 lines.

 TheFileLines.AddRange(System.IO.File.ReadAllLines(FileAddress))

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial