Trying to close textfile after line is readWhat is the difference between File.ReadLines() and File.ReadAllLines()?DataGridView not Refreshing/Updating/Reloading Data. After Child form closesread line after specific delimiterHow to Update the selected Data in datagridview after it sorted/order?Program trying to read lines that aren't thereDataGridView skipping lines VB.netRead code from TextFile and execute in VBSplit array and read to specific lineHow do you delete empty lines from a textfileview each line of RichTextBox in TextBox1 one by one if second line appears to TextBox1 the First line will disappearReading second line of textfile and adding to datagridview

Next date with distinct digits

If I leave the US through an airport, do I have to return through the same airport?

Why was this person allowed to become Grand Maester?

How can one's career as a reviewer be ended?

My boss want to get rid of me - what should I do?

What are neighboring ports?

What are some really overused phrases in French that are common nowadays?

Proving that a Russian cryptographic standard is too structured

Why are MBA programs closing?

What would be the way to say "just saying" in German? (Not the literal translation)

Is it possible to have a wealthy country without a middle class?

A word that means "blending into a community too much"

Why not invest in precious metals?

With Ubuntu 18.04, how can I have a hot corner that locks the computer?

Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?

Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?

Why is there always a fire truck present before refuelling?

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

Can the removal of a duty-free sales trolley result in a measurable reduction in emissions?

Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?

Does Assassinate grant two attacks?

What does 思ってやっている mean?

Are there any normal animals in Pokemon universe?

Code downloads a text file from a website, saves it to local disk, and then loads it into a list for further processing



Trying to close textfile after line is read


What is the difference between File.ReadLines() and File.ReadAllLines()?DataGridView not Refreshing/Updating/Reloading Data. After Child form closesread line after specific delimiterHow to Update the selected Data in datagridview after it sorted/order?Program trying to read lines that aren't thereDataGridView skipping lines VB.netRead code from TextFile and execute in VBSplit array and read to specific lineHow do you delete empty lines from a textfileview each line of RichTextBox in TextBox1 one by one if second line appears to TextBox1 the First line will disappearReading second line of textfile and adding to datagridview






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















Im trying to output the data from the second line of my textfile to a datagridview but when doing so it is also outputting every line after the the second line. This is what I have tried. Thanks



 Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
For index = 1 To lines.Length - 1
Dim cells = lines(index).Split(","c)
dgvOutput.Rows.Add(cells)
FileClose()









share|improve this question






















  • where is the Next? please provide a minimum, complete, verifiable example.

    – FalcoGer
    Mar 24 at 20:01











  • Please could you explain what is different in this question from your previous one. Do you want to read all the lines starting from the second one or just the second line?

    – Steve
    Mar 24 at 20:13

















-1















Im trying to output the data from the second line of my textfile to a datagridview but when doing so it is also outputting every line after the the second line. This is what I have tried. Thanks



 Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
For index = 1 To lines.Length - 1
Dim cells = lines(index).Split(","c)
dgvOutput.Rows.Add(cells)
FileClose()









share|improve this question






















  • where is the Next? please provide a minimum, complete, verifiable example.

    – FalcoGer
    Mar 24 at 20:01











  • Please could you explain what is different in this question from your previous one. Do you want to read all the lines starting from the second one or just the second line?

    – Steve
    Mar 24 at 20:13













-1












-1








-1








Im trying to output the data from the second line of my textfile to a datagridview but when doing so it is also outputting every line after the the second line. This is what I have tried. Thanks



 Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
For index = 1 To lines.Length - 1
Dim cells = lines(index).Split(","c)
dgvOutput.Rows.Add(cells)
FileClose()









share|improve this question














Im trying to output the data from the second line of my textfile to a datagridview but when doing so it is also outputting every line after the the second line. This is what I have tried. Thanks



 Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
For index = 1 To lines.Length - 1
Dim cells = lines(index).Split(","c)
dgvOutput.Rows.Add(cells)
FileClose()






vb.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 19:46









PadraigPadraig

11




11












  • where is the Next? please provide a minimum, complete, verifiable example.

    – FalcoGer
    Mar 24 at 20:01











  • Please could you explain what is different in this question from your previous one. Do you want to read all the lines starting from the second one or just the second line?

    – Steve
    Mar 24 at 20:13

















  • where is the Next? please provide a minimum, complete, verifiable example.

    – FalcoGer
    Mar 24 at 20:01











  • Please could you explain what is different in this question from your previous one. Do you want to read all the lines starting from the second one or just the second line?

    – Steve
    Mar 24 at 20:13
















where is the Next? please provide a minimum, complete, verifiable example.

– FalcoGer
Mar 24 at 20:01





where is the Next? please provide a minimum, complete, verifiable example.

– FalcoGer
Mar 24 at 20:01













Please could you explain what is different in this question from your previous one. Do you want to read all the lines starting from the second one or just the second line?

– Steve
Mar 24 at 20:13





Please could you explain what is different in this question from your previous one. Do you want to read all the lines starting from the second one or just the second line?

– Steve
Mar 24 at 20:13












3 Answers
3






active

oldest

votes


















1














It's outputting every line after the second line, because that's what you're telling it to do when you iterate through the array of strings returns from ReadAllLines.



IO.File.ReadAllLines does not leave an output stream open. The file is closed. What it does do, is return a zero-based (by default) array of the contents of the file, with line breaks being the delimiter for the split.



To just get the contents of the second line, using ReadAllLines, this is what you need:



Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
If lines.length >= 2 Then
Dim cells = lines(1).Split(","c)
dgvOutput.Rows.Add(cells)
End If


Now, that does have the overhead of reading the entire file in. If you open the file using a reader object, then you only need to read the first and second lines of the file to get that second line.



That would be something like this:



Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(OrderId & ".txt")
Dim a as String
' This reads the first line, which we throw away
reader.ReadLine()
a = reader.ReadLine()
reader.Close()
Dim cells = a.Split(","c)
dgvOutput.Rows.Add(cells)


You would need to test your explicit circumstances to determine which is better for what you're trying to do.






share|improve this answer






























    1














    Your loop is executed over all lines skipping just the first line.

    While I cannot see what happen in the FileClose call it seems to not have any sense because ReadAllLines has already closed the file.



    You can get the second line of your file with a single line of code



    Dim line as String = File.ReadLines(OrderID & ".txt").Skip(1).Take(1).FirstOrDefault()

    ' this check is required to avoid problems with files containing 0 or 1 line
    if line IsNot Nothing Then
    Dim cells = line.Split(","c)
    dgvOutput.Rows.Add(cells)
    End If


    Notice that I have replaced the ReadAllLines with ReadLines. This is better because using this method you don't read all lines when you need only the second one (if it exists). More info at ReadLines vs ReadAllLines






    share|improve this answer

























    • One small point: as you're calling Take(1) then there can never be more than one line, so SingleOrDefault would be more appropriate than FirstOrDefault.

      – jmcilhinney
      Mar 24 at 21:35


















    0














    Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
    Dim SecondLine = lines(1)


    File.ReadAllLines opens and closes the file for you so there is not need to add code to close it.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327867%2ftrying-to-close-textfile-after-line-is-read%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      It's outputting every line after the second line, because that's what you're telling it to do when you iterate through the array of strings returns from ReadAllLines.



      IO.File.ReadAllLines does not leave an output stream open. The file is closed. What it does do, is return a zero-based (by default) array of the contents of the file, with line breaks being the delimiter for the split.



      To just get the contents of the second line, using ReadAllLines, this is what you need:



      Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
      If lines.length >= 2 Then
      Dim cells = lines(1).Split(","c)
      dgvOutput.Rows.Add(cells)
      End If


      Now, that does have the overhead of reading the entire file in. If you open the file using a reader object, then you only need to read the first and second lines of the file to get that second line.



      That would be something like this:



      Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(OrderId & ".txt")
      Dim a as String
      ' This reads the first line, which we throw away
      reader.ReadLine()
      a = reader.ReadLine()
      reader.Close()
      Dim cells = a.Split(","c)
      dgvOutput.Rows.Add(cells)


      You would need to test your explicit circumstances to determine which is better for what you're trying to do.






      share|improve this answer



























        1














        It's outputting every line after the second line, because that's what you're telling it to do when you iterate through the array of strings returns from ReadAllLines.



        IO.File.ReadAllLines does not leave an output stream open. The file is closed. What it does do, is return a zero-based (by default) array of the contents of the file, with line breaks being the delimiter for the split.



        To just get the contents of the second line, using ReadAllLines, this is what you need:



        Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
        If lines.length >= 2 Then
        Dim cells = lines(1).Split(","c)
        dgvOutput.Rows.Add(cells)
        End If


        Now, that does have the overhead of reading the entire file in. If you open the file using a reader object, then you only need to read the first and second lines of the file to get that second line.



        That would be something like this:



        Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(OrderId & ".txt")
        Dim a as String
        ' This reads the first line, which we throw away
        reader.ReadLine()
        a = reader.ReadLine()
        reader.Close()
        Dim cells = a.Split(","c)
        dgvOutput.Rows.Add(cells)


        You would need to test your explicit circumstances to determine which is better for what you're trying to do.






        share|improve this answer

























          1












          1








          1







          It's outputting every line after the second line, because that's what you're telling it to do when you iterate through the array of strings returns from ReadAllLines.



          IO.File.ReadAllLines does not leave an output stream open. The file is closed. What it does do, is return a zero-based (by default) array of the contents of the file, with line breaks being the delimiter for the split.



          To just get the contents of the second line, using ReadAllLines, this is what you need:



          Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
          If lines.length >= 2 Then
          Dim cells = lines(1).Split(","c)
          dgvOutput.Rows.Add(cells)
          End If


          Now, that does have the overhead of reading the entire file in. If you open the file using a reader object, then you only need to read the first and second lines of the file to get that second line.



          That would be something like this:



          Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(OrderId & ".txt")
          Dim a as String
          ' This reads the first line, which we throw away
          reader.ReadLine()
          a = reader.ReadLine()
          reader.Close()
          Dim cells = a.Split(","c)
          dgvOutput.Rows.Add(cells)


          You would need to test your explicit circumstances to determine which is better for what you're trying to do.






          share|improve this answer













          It's outputting every line after the second line, because that's what you're telling it to do when you iterate through the array of strings returns from ReadAllLines.



          IO.File.ReadAllLines does not leave an output stream open. The file is closed. What it does do, is return a zero-based (by default) array of the contents of the file, with line breaks being the delimiter for the split.



          To just get the contents of the second line, using ReadAllLines, this is what you need:



          Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
          If lines.length >= 2 Then
          Dim cells = lines(1).Split(","c)
          dgvOutput.Rows.Add(cells)
          End If


          Now, that does have the overhead of reading the entire file in. If you open the file using a reader object, then you only need to read the first and second lines of the file to get that second line.



          That would be something like this:



          Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(OrderId & ".txt")
          Dim a as String
          ' This reads the first line, which we throw away
          reader.ReadLine()
          a = reader.ReadLine()
          reader.Close()
          Dim cells = a.Split(","c)
          dgvOutput.Rows.Add(cells)


          You would need to test your explicit circumstances to determine which is better for what you're trying to do.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 at 19:58









          Stephen WrightonStephen Wrighton

          28.1k55984




          28.1k55984























              1














              Your loop is executed over all lines skipping just the first line.

              While I cannot see what happen in the FileClose call it seems to not have any sense because ReadAllLines has already closed the file.



              You can get the second line of your file with a single line of code



              Dim line as String = File.ReadLines(OrderID & ".txt").Skip(1).Take(1).FirstOrDefault()

              ' this check is required to avoid problems with files containing 0 or 1 line
              if line IsNot Nothing Then
              Dim cells = line.Split(","c)
              dgvOutput.Rows.Add(cells)
              End If


              Notice that I have replaced the ReadAllLines with ReadLines. This is better because using this method you don't read all lines when you need only the second one (if it exists). More info at ReadLines vs ReadAllLines






              share|improve this answer

























              • One small point: as you're calling Take(1) then there can never be more than one line, so SingleOrDefault would be more appropriate than FirstOrDefault.

                – jmcilhinney
                Mar 24 at 21:35















              1














              Your loop is executed over all lines skipping just the first line.

              While I cannot see what happen in the FileClose call it seems to not have any sense because ReadAllLines has already closed the file.



              You can get the second line of your file with a single line of code



              Dim line as String = File.ReadLines(OrderID & ".txt").Skip(1).Take(1).FirstOrDefault()

              ' this check is required to avoid problems with files containing 0 or 1 line
              if line IsNot Nothing Then
              Dim cells = line.Split(","c)
              dgvOutput.Rows.Add(cells)
              End If


              Notice that I have replaced the ReadAllLines with ReadLines. This is better because using this method you don't read all lines when you need only the second one (if it exists). More info at ReadLines vs ReadAllLines






              share|improve this answer

























              • One small point: as you're calling Take(1) then there can never be more than one line, so SingleOrDefault would be more appropriate than FirstOrDefault.

                – jmcilhinney
                Mar 24 at 21:35













              1












              1








              1







              Your loop is executed over all lines skipping just the first line.

              While I cannot see what happen in the FileClose call it seems to not have any sense because ReadAllLines has already closed the file.



              You can get the second line of your file with a single line of code



              Dim line as String = File.ReadLines(OrderID & ".txt").Skip(1).Take(1).FirstOrDefault()

              ' this check is required to avoid problems with files containing 0 or 1 line
              if line IsNot Nothing Then
              Dim cells = line.Split(","c)
              dgvOutput.Rows.Add(cells)
              End If


              Notice that I have replaced the ReadAllLines with ReadLines. This is better because using this method you don't read all lines when you need only the second one (if it exists). More info at ReadLines vs ReadAllLines






              share|improve this answer















              Your loop is executed over all lines skipping just the first line.

              While I cannot see what happen in the FileClose call it seems to not have any sense because ReadAllLines has already closed the file.



              You can get the second line of your file with a single line of code



              Dim line as String = File.ReadLines(OrderID & ".txt").Skip(1).Take(1).FirstOrDefault()

              ' this check is required to avoid problems with files containing 0 or 1 line
              if line IsNot Nothing Then
              Dim cells = line.Split(","c)
              dgvOutput.Rows.Add(cells)
              End If


              Notice that I have replaced the ReadAllLines with ReadLines. This is better because using this method you don't read all lines when you need only the second one (if it exists). More info at ReadLines vs ReadAllLines







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 24 at 20:07

























              answered Mar 24 at 19:56









              SteveSteve

              185k16166225




              185k16166225












              • One small point: as you're calling Take(1) then there can never be more than one line, so SingleOrDefault would be more appropriate than FirstOrDefault.

                – jmcilhinney
                Mar 24 at 21:35

















              • One small point: as you're calling Take(1) then there can never be more than one line, so SingleOrDefault would be more appropriate than FirstOrDefault.

                – jmcilhinney
                Mar 24 at 21:35
















              One small point: as you're calling Take(1) then there can never be more than one line, so SingleOrDefault would be more appropriate than FirstOrDefault.

              – jmcilhinney
              Mar 24 at 21:35





              One small point: as you're calling Take(1) then there can never be more than one line, so SingleOrDefault would be more appropriate than FirstOrDefault.

              – jmcilhinney
              Mar 24 at 21:35











              0














              Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
              Dim SecondLine = lines(1)


              File.ReadAllLines opens and closes the file for you so there is not need to add code to close it.






              share|improve this answer



























                0














                Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
                Dim SecondLine = lines(1)


                File.ReadAllLines opens and closes the file for you so there is not need to add code to close it.






                share|improve this answer

























                  0












                  0








                  0







                  Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
                  Dim SecondLine = lines(1)


                  File.ReadAllLines opens and closes the file for you so there is not need to add code to close it.






                  share|improve this answer













                  Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
                  Dim SecondLine = lines(1)


                  File.ReadAllLines opens and closes the file for you so there is not need to add code to close it.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 25 at 6:13









                  MaryMary

                  4,74121022




                  4,74121022



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327867%2ftrying-to-close-textfile-after-line-is-read%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                      155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해