System.IO.FileNotFoundException: 'Could not find file

Brent Bell 0 Reputation points
2023-02-23T19:33:47.9966667+00:00

Just started using Visual Studio. I'm trying to run the sample program below. I added a data file (values.text) to the Solution and in the Properties window I set Copy to Output Directory to Copy Always. I can see the file in the ...bin\Debug\net6.0 folder but I still get the error message below:

System.IO.FileNotFoundException: 'Could not find file 'C:\Users\brbell\source\repos\ReadTextFileWhile\bin\Debug\net6.0\values.text'.'

Any ideas?

Imports System
Imports System.IO

Module Program
    Sub Main()
        Dim myReader As StreamReader = New StreamReader("values.text")
        Dim line As String = ""

        While Not IsNothing(line)
            line = myReader.ReadLine()
            If Not IsNothing(line) Then
                Console.WriteLine(line)
            End If
        End While

        myReader.Close()
        Console.ReadLine()

    End Sub
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,805 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,201 Reputation points Microsoft External Staff
    2023-02-24T07:21:05.1366667+00:00

    Hi , welcome to Microsoft Q&A!

    I used your code for testing and the same error did not occur.

    Please check your file name to see if it is values.txt not values.text

    Best Regards.
    Jiachen Li
    ----------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.