Skip to content

Commit 0fdcc8c

Browse files
CopilotBillWagnergewarren
authored
Fix Visual Basic file reading examples: remove hardcoded paths, add RichTextBox support, and include project file (#48474)
* Initial plan * Fix file path handling and add RichTextBox examples for Visual Basic file reading Co-authored-by: BillWagner <[email protected]> * Add VbFileIORead.vbproj for CI build Co-authored-by: BillWagner <[email protected]> * Update samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbFileIORead/VB/VbFileIORead.vbproj Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 9a6e16d commit 0fdcc8c

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

docs/visual-basic/developing-apps/programming/drives-directories-files/how-to-read-from-text-files.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ Use the `ReadAllText` method of the `My.Computer.FileSystem` object to read the
3131

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

34+
### To read from a text file into a RichTextBox control
35+
36+
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.
37+
38+
[!code-vb[VbFileIORead#21](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbFileIORead/VB/Class1.vb#21)]
39+
40+
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:
41+
42+
[!code-vb[VbFileIORead#22](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbFileIORead/VB/Class1.vb#22)]
43+
44+
> [!NOTE]
45+
> 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.
46+
3447
## Robust Programming
3548

3649
The following conditions may cause an exception:

samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbFileIORead/VB/Class1.vb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,45 @@ Class Class735fe9d70f7a4185ba02f35e580ec4b8
2020
Public Sub Method2()
2121
' <snippet2>
2222
Dim fileReader As String
23-
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
23+
fileReader = My.Computer.FileSystem.ReadAllText("test.txt")
2424
MsgBox(fileReader)
2525
' </snippet2>
2626
End Sub
2727

2828
Public Sub Method3()
2929
' <snippet3>
3030
Dim fileReader As String
31-
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt",
31+
fileReader = My.Computer.FileSystem.ReadAllText("test.txt",
3232
System.Text.Encoding.UTF32)
3333
MsgBox(fileReader)
3434
' </snippet3>
3535
End Sub
3636

37+
Public Sub LoadTextIntoRichTextBox()
38+
' <snippet21>
39+
' Load text file into a RichTextBox control
40+
' Note: This assumes RichTextBox1 is a control on your form
41+
Dim fileText As String
42+
fileText = My.Computer.FileSystem.ReadAllText("test.txt")
43+
' RichTextBox1.Text = fileText
44+
' </snippet21>
45+
End Sub
46+
47+
Public Sub LoadTextIntoRichTextBoxWithPath()
48+
' <snippet22>
49+
' Load text file into a RichTextBox control using a specific path
50+
Try
51+
Dim filePath As String = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "test.txt")
52+
Dim fileText As String = My.Computer.FileSystem.ReadAllText(filePath)
53+
' RichTextBox1.Text = fileText
54+
Catch ex As System.IO.FileNotFoundException
55+
MsgBox("File not found: " & ex.Message)
56+
Catch ex As Exception
57+
MsgBox("Error reading file: " & ex.Message)
58+
End Try
59+
' </snippet22>
60+
End Sub
61+
3762
End Class
3863

3964
Class Class8d185eb279ca42cd95a7d3ff44a5a0f8
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net4.81</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a sample text file for testing the file reading examples.
2+
It contains multiple lines of text that can be read into a string variable
3+
or loaded directly into a RichTextBox control.

0 commit comments

Comments
 (0)