Zapis i odczyt z pliku TXT - kompletne rozwiązanie

VB.NET
Copy to clipboard
specta
  1. Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
  2. Dim i As Integer
  3. Dim aryText(4) As String
  4.  
  5. aryText(0) = "Mary WriteLine"
  6. aryText(1) = "Had"
  7. aryText(2) = "A"
  8. aryText(3) = "Little"
  9. aryText(4) = "One"
  10.  
  11. Dim objWriter As New System.IO.StreamWriter( FILE_NAME )
  12.  
  13. For i = 0 To 4
  14.  
  15. objWriter.WriteLine( aryText(i) )
  16.  
  17. Next
  18.  
  19. objWriter.Close()
  20.  
  21. ----------------------------
  22.  
  23. Zapisanie do pliku tekstu - jeżeli plik istnieje:
  24.  
  25. Dim FILE_NAME As String = "C:\Users\Owner\Documents\test2.txt"
  26.  
  27. If System.IO.File.Exists(FILE_NAME) = True Then
  28.  
  29. Dim objWriter As New System.IO.StreamWriter( FILE_NAME )
  30.  
  31. objWriter.Write( TextBox1.Text )
  32. objWriter.Close()
  33. MsgBox("Text written to file")
  34.  
  35. Else
  36.  
  37. MsgBox("File Does Not Exist")
  38.  
  39. End If
  40.  
  41. -----------------------------------
  42. Odczyt z pliku jeżeli istnieje - całość tekstu do końca pliku.
  43.  
  44. Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
  45.  
  46. If System.IO.File.Exists(FILE_NAME) = True Then
  47.  
  48. Dim objReader As New System.IO.StreamReader(FILE_NAME)
  49. TextBox1.Text = objReader.ReadToEnd
  50. objReader.Close()
  51.  
  52. Else
  53.  
  54. MsgBox("File Does Not Exist")
  55.  
  56. End If>
  57.  
  58. ---------------------------------------
  59. Odczyt linijka po linijce
  60.  
  61. Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
  62.  
  63. Dim TextLine As String
  64.  
  65. If System.IO.File.Exists( FILE_NAME ) = True Then
  66.  
  67. Dim objReader As New System.IO.StreamReader(FILE_NAME)
  68.  
  69. Do While objReader.Peek() <> -1
  70.  
  71. TextLine = TextLine & objReader.ReadLine() & vbNewLine
  72.  
  73. Loop
  74.  
  75. Textbox1.Text = TextLine
  76.  
  77. Else
  78.  
  79. MsgBox("File Does Not Exist")
  80.  
  81. End If

    Leave a comment

     
    Visual verification
     

    Related books

    More InfoLearning Visual Basic .Net
    Learning Visual Basic .Net
    More InfoBeginning Visual Basic 2015
    Beginning Visual Basic 2015
    More InfoProgramming .NET Components, 2nd Edition
    Programming .NET Components, 2nd Edition
    More InfoPractical Database Programming with Visual Basic.NET
    Practical Database Programming with Visual Basic.NET
    More InfoVisual Basic .NET All-In-One Desk Reference For Dummies
    Visual Basic .NET All-In-One Desk Reference For Dummies
    More InfoMurach's ASP.NET 4.5 Web Programming with VB 2012 (Training & Reference)
    Murach's ASP.NET 4.5 Web Programming with VB 2012 (Training & Reference)
    More InfoVisual Basic .NET Programming
    Visual Basic .NET Programming
    More InfoDatabase Programming with Visual Basic .NET
    Database Programming with Visual Basic .NET