C# C# Streams and Data Processing Reading Data Console I/O

Console I/O

This code does not work, it throws an error cannot convert from 'string' to 'System.IO.Stream'

on this line

using (var reader = new StreamReader(file.FullName)) {

3 Answers

Steven Parker
Steven Parker
186,461 Points

This may require more complete code to analyze.

At first look, this line seems fine. What's even more puzzling is that the constructor for StreamReader is overloaded, and it can take a Stream or a string as an argument, so it should not be trying to convert one to the other.

Even though the error occurs on this line, the actual cause might be elsewhere in the code. Can you post a link to your workspace snapshot or github repo for a more detailed analysis?

Hi, sure.

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string currentDirectory = Directory.GetCurrentDirectory();
        DirectoryInfo directory = new DirectoryInfo(currentDirectory);
        var fileName = Path.Combine(directory.FullName, "data.txt");
        var file = new FileInfo(fileName);
        if (file.Exists)
        {
            using (var reader = new StreamReader(file.FullName)) { 
                Console.SetIn(reader);
                Console.WriteLine(Console.ReadLine());
            }
        }
    }
}

I just had a look in the StreamReader class and I don't see a overloaded constructor that takes a string...

// Constructors
        //
        public StreamReader (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);

        public StreamReader (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);

        public StreamReader (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks);

        public StreamReader (Stream stream, Encoding encoding);

        public StreamReader (Stream stream, bool detectEncodingFromByteOrderMarks);

        public StreamReader (Stream stream);
Steven Parker
Steven Parker
186,461 Points

Where did you find that list of constructors? That's only 6 of the 11 that I found. Are you perhaps working with some obsolete assemblies?

I was looking on MSDN here.

Yeah I know confusing , I believe it's something to do with portable class library as I only have the constructors included in it? but as a noob mac user I don't really understand.

I got it from ctrl clicking the class

Steven Parker
Steven Parker
186,461 Points

It does sound like you need a newer version of the System.Io assembly.

Your current version of StreamReader won't work as used in the video. As a work-around you might try replacing "file.FullName" with "new FileStream(file.FullName, FileMode.Open)".

It seems strange as it was only downloaded recently so i downloaded it again and still have the same problem, If you look here under .net framework 4 version (I assume thats what i have?) https://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.100).aspx you can see the missing constructors are the ones that are "not supported by the portable class library" I assume this is the root of the issue although I don't fully understand how when or why this is implemented?

This doesn't work in visual studio or visual studio code but it does work in xamarin studio... Whats going on why are the constructors available in xamarin studio?

Steven Parker
Steven Parker
186,461 Points

Does my work-around suggestion help?

I can only guess that Xamarin studio comes with a more current version of System.IO.

It does but I'm hoping for a solution rather than a work around, I could have thousands of missing constructors which will cause lots of problems...

Henrik Christensen
Henrik Christensen
Python Web Development Techdegree Student 38,299 Points

I'm was having the same problem, but I made it work by using the work-around provided by Steven (in VS2017 community):

static void Main(string[] args)
{
    string currentDirectory = Directory.GetCurrentDirectory();
    DirectoryInfo directoryInfo = new DirectoryInfo(currentDirectory);
    string fileName = Path.Combine(directoryInfo.FullName, "data.txt");
    FileInfo file = new FileInfo(fileName);

    if (file.Exists)
    {
        using (FileStream fs = new FileStream(file.FullName, FileMode.Open))
        {
            using (StreamReader sr = new StreamReader(fs))
            {

            }
        }
    }
}
Samuel Joseph
Samuel Joseph
5,406 Points

Have you tried installing the latest version via Nuget Package Manager?

https://www.nuget.org/packages/System.IO/