Question

I am trying to open a text file (example.txt) that is on the desktop with a StreamReader. But when I run my code the error message that it throws is: Could not find file (whatever code path, leading all the way to the debug folder in my project). So I am wondering, is the Streamder the wrong tool for the job?

This is the code that I am using.

Console.WriteLine("What file would you like to load?");
string FileName = Console.ReadLine();

StreamReader Reader - new StreamReader(FileName);

The exception gets thrown at the StreamReader

Was it helpful?

Solution

It's likely that your program couldn't find the file because it is looking in the wrong place. You can correct that with something like this...

    private void ReadFromDesktop(string fileName)
    {
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string fullName = System.IO.Path.Combine(desktopPath, fileName);
        using (StreamReader steamReader = new StreamReader(fullName))
        {
            string content = steamReader.ReadToEnd();
        }
    }

This method takes the name of a file (presumably that which you entered on the console) and then queries the environment for the path to your desktop.

Then it uses the Path.Combine method to create a fully qualified name for the file. The result is processed by the stream reader.

The point being that if you provide only the file name, the app will look in its root directory (which is, as you wrote, the debug directory). The hint for that was the error message telling you that it created a full path by using the name of your debug directory.

OTHER TIPS

Try this instead:

var reader = File.OpenText(FileName);

Or for small files:

var allText = File.ReadAllText(FileName);

Of course... it's always possible the FileName specified DOESN'T actually exist.... right? I mean, by default it's probably going to look for a file locally next to the .exe which will be in /.../.../bin/debug/ or something.

If you specifically want to look for file on the current user's desktop folder, you might need:

var fullPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + FileName;
var allText = File.ReadAllText(fullPath);

"Could not find file" is actually what was happened. Pay attention to the exception details and check you're entering existing file name along with extension. Also don't forget to dispose the StreamReader:

    string text;
    var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName);
    using (var sr = new StreamReader(path))
    {
        text = sr.ReadToEnd();
    }

There is no obvious error in your code (assuming that you used correct syntax and not the actual code in your post) but it is possible that the error is due to an incorrect path.

If the file exists and you use an absolute path (beginning with the drive letter on windows) you should be able to do something like this:

        Console.WriteLine("What file would you like to load?");
        string fileName = Console.ReadLine();

        using (Stream reader = new StreamReader(fileName))
        {
            string line = reader.ReadLine();
            // ...
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top