Docs: StreamReader could not find file path

Created on 11 Jul 2020  Â·  6Comments  Â·  Source: dotnet/docs

Greetings,

I'm debugging (the F5 option) in Visual Studio 2019 on Windows 10 pro, and StreamReader here is throwing a runtime error, saying it can't find the path.
c# using (StreamReader sr = new StreamReader("TestFile.txt"))
I've tried several paths now, including the one on the StreamWriter doc page. Is this because I used the GUI to manipulate TestFile.txt??

Let me know if I can supply further information.

Thanks!


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - .NET Guide support-request

All 6 comments

Are you sure the file exists in the same directory of the executable?

No, the file is in the default directory specified by the StreamReader
constructor: ~projectbinDebugnetcoreapp3.1

I have also tried other paths, including ~project and
~OneDriveDocuments

I actually don't see any executable anywhere in the project directory, not
even hidden. Is it only a temporary file when using Visual Studio's debug
feature for C#? I remember C++ overwrote a permanent .exe for each build,
but that's all I know...

Thompson

On Sat, Jul 11, 2020 at 12:00 PM Youssef Victor notifications@github.com
wrote:

Are you sure the file exists in the same directory of the executable?

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/docs/issues/19434#issuecomment-657086020, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AC4EBBQTYUKBUKP7BAGYGLDR3CECPANCNFSM4OXK4KZA
.

Hi 👋 @cyberTrebuchet
Thank you so much for posting this issue. We really appreciate it.

When you use a path like that "TestFile.txt", it assumes that it is relative to the current directory in which the executable is running. In this case, would you be able to add some output debug details?

Just before the line that is the using (StreamReader sr = new StreamReader("TestFile.txt")), add the following:

Console.WriteLine(File.Exists("TestFile.txt"));
Console.WriteLine(Path.GetFullPath(Directory.GetCurrentDirectory()));
foreach (var file in Directory.EnumerateFiles(Directory.GetCurrentDirectory()))
{
    Console.WriteLine(file);
}

Then let me know what is output, I'll use this to try to help you and we'll go from there.

Here is the console output from your code:

C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1
C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1\factorer.deps.json
C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1\factorer.dll
C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1\factorer.exe
C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1\factorer.pdb
C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1\factorer.runtimeconfig.dev.json
C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1\factorer.runtimeconfig.json
C:\Users\cyber\source\repos\factorer\factorer\bin\Debug\netcoreapp3.1\TestFile.txt.txt

As you can see, the problem was indeed my use of the GUI to create my test
file. The GUI was hiding the extra file extension. Serves me right. Thanks
for your help!

Thompson

On Mon, Jul 13, 2020 at 8:49 AM David Pine notifications@github.com wrote:

Hi 👋 @cyberTrebuchet https://github.com/cyberTrebuchet
Thank you so much for posting this issue. We really appreciate it.

When you use a path like that "TestFile.txt", it assumes that it is
relative to the current directory in which the executable is running. In
this case, would you be able to add some output debug details?

Just before the line that is the using (StreamReader sr = new
StreamReader("TestFile.txt")), add the following:

Console.WriteLine(File.Exists("TestFile.txt"));
Console.WriteLine(Path.GetFullPath(Directory.GetCurrentDirectory()));
foreach (var file in Directory.EnumerateFiles(Directory.GetCurrentDirectory()))

{

Console.WriteLine(file);

}

Then let me know what is output, I'll use this to try to help you and
we'll go from there.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/docs/issues/19434#issuecomment-657540647, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AC4EBBTX3BITAKP7MQCXVQTR3L7ELANCNFSM4OXK4KZA
.

@cyberTrebuchet, A small tip to avoid a problem similar to this later. From file explorer, click view, then check File name extensions.

image

This will make the file appear when the full name with extensions.

Oh there it is! First time coding on Windows! Thanks again!

Thompson

On Mon, Jul 13, 2020 at 12:44 PM Youssef Victor notifications@github.com
wrote:

@cyberTrebuchet https://github.com/cyberTrebuchet, A small tip to avoid
a problem similar to this later. From file explorer, click view, then check
File name extensions.

[image: image]
https://user-images.githubusercontent.com/31348972/87330317-b5f82380-c538-11ea-9e32-f76ba9c49ff1.png

This will make the file appear when the full name with extensions.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/docs/issues/19434#issuecomment-657668226, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AC4EBBR4VO4QIJI3AU2OPALR3M2VFANCNFSM4OXK4KZA
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebagomez picture sebagomez  Â·  3Comments

stanuku picture stanuku  Â·  3Comments

ike86 picture ike86  Â·  3Comments

LJ9999 picture LJ9999  Â·  3Comments

gmatv picture gmatv  Â·  3Comments