Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ Use the `ReadAllText` method of the `My.Computer.FileSystem` object to read the

[!code-vb[VbFileIORead#3](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbFileIORead/VB/Class1.vb#3)]

### To read from a text file into a RichTextBox control

To load the contents of a text file directly into a RichTextBox control, read the file contents into a string and assign it to the `Text` property of the RichTextBox. The following example shows how to read a text file and load it into a RichTextBox control.

[!code-vb[VbFileIORead#21](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbFileIORead/VB/Class1.vb#21)]

For better error handling and file path management, you can use the following approach that constructs a proper file path and handles potential exceptions. This approach avoids hardcoded drive paths that can cause issues on different systems:

[!code-vb[VbFileIORead#22](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbFileIORead/VB/Class1.vb#22)]

> [!NOTE]
> When specifying file paths, avoid using hardcoded absolute paths like "C:\temp\file.txt" as these can cause issues on systems where the drive letter or directory structure is different. Instead, use relative paths or construct paths using <xref:System.IO.Path.Combine%2A> to ensure your code works across different environments.
## Robust Programming

The following conditions may cause an exception:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,45 @@ Class Class735fe9d70f7a4185ba02f35e580ec4b8
Public Sub Method2()
' <snippet2>
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
fileReader = My.Computer.FileSystem.ReadAllText("test.txt")
MsgBox(fileReader)
' </snippet2>
End Sub

Public Sub Method3()
' <snippet3>
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt",
fileReader = My.Computer.FileSystem.ReadAllText("test.txt",
System.Text.Encoding.UTF32)
MsgBox(fileReader)
' </snippet3>
End Sub

Public Sub LoadTextIntoRichTextBox()
' <snippet21>
' Load text file into a RichTextBox control
' Note: This assumes RichTextBox1 is a control on your form
Dim fileText As String
fileText = My.Computer.FileSystem.ReadAllText("test.txt")
' RichTextBox1.Text = fileText
' </snippet21>
End Sub

Public Sub LoadTextIntoRichTextBoxWithPath()
' <snippet22>
' Load text file into a RichTextBox control using a specific path
Try
Dim filePath As String = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "test.txt")
Dim fileText As String = My.Computer.FileSystem.ReadAllText(filePath)
' RichTextBox1.Text = fileText
Catch ex As System.IO.FileNotFoundException
MsgBox("File not found: " & ex.Message)
Catch ex As Exception
MsgBox("Error reading file: " & ex.Message)
End Try
' </snippet22>
End Sub

End Class

Class Class8d185eb279ca42cd95a7d3ff44a5a0f8
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net4.81</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a sample text file for testing the file reading examples.
It contains multiple lines of text that can be read into a string variable
or loaded directly into a RichTextBox control.
Loading