0

Using the dotnet-cli (dotnet new, dotnet restore) with VScode, I made a new C# program.

However, I can't seem to use the StreamReader properly. Here's the code.

using System;
using System.IO;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            StreamReader test = new StreamReader("Test.txt");
        }
    }
}

I can't seem to run this program. When I run using dotnet run, it says that

'string' cannot be converted to 'System.IO.Stream' [netcoreapp1.0]

I tried creating the same program in Visual Studio Community and it runs fine with out any errrors

jlawcordova
  • 147
  • 3
  • 9

1 Answers1

1

To solve your problem: You have to use a Stream as basic access to the file:

using(var fs = new FileStream("file.txt", FileMode.Open, FileAccess.Read))
    using (var sr = new System.IO.StreamReader(fs)){
        //Read file via sr.Read(), sr.ReadLine, ...
    }
}

Since StreamReader and FileStream implement IDisposable, they will be disposed of because of the using clauses so you don't need to write a call .Close() or .Dispose() (As @TaW said).

Radinator
  • 1,048
  • 18
  • 58
  • This does not seem to work. Code lints appear stating that `non-invocable member 'FileStream' cannot be used like a method.'` – jlawcordova Sep 14 '16 at 07:02
  • There's a typo in your code. It should be `var fs = new System.IO.FileStream(...` – Abion47 Sep 14 '16 at 07:04
  • Jep...sry :( Just did it on-the-fly with Notepad++ – Radinator Sep 14 '16 at 07:05
  • And now it says, `'AccessMode' does not exist in the current context [netcoreapp1.0]` – jlawcordova Sep 14 '16 at 07:21
  • I missed it myself at the glance, but he combined the `FileMode` and `FileAccess` classes into a single object. :P – Abion47 Sep 14 '16 at 07:25
  • It works now! Thank you! Just out of curiosity, may I know why in VS Community, StreamReader doesn't need to have a FileStream as args, just a string of the file path? – jlawcordova Sep 14 '16 at 07:32
  • 1
    depends on what framework your VSCode bases. in .NET FW using in VS2015 CE the constructor of StreamReader takes the path or a stream – Radinator Sep 14 '16 at 07:33
  • Oh I see. Thanks again! – jlawcordova Sep 14 '16 at 07:38
  • Your explanation at the end seems wrong. The `using` clause does the job of disposing here. Otherwise: you actually [__should__ call `Dispose`](https://msdn.microsoft.com/en-us/library/3bwa4xa9%28v=vs.110%29.aspx) on those objects! – TaW Sep 14 '16 at 08:31
  • I don't think so: __The using statement calls the Dispose method on the object in the correct way__ (https://msdn.microsoft.com/en-us/library/yh598w02.aspx). And if you look in the .NET source code, you will see that Close() calls the .Dispose() methode and .Dispose() calls .Close() (http://stackoverflow.com/a/7525134/6635287). Even if the answer in the last link tells you at the end that closing the stream before exiting the using block "is best to do this", this is just syntatctic sugar. As Enigmativity says: **It doesn't affect the behaviour of the code, but it does aid readability.** – Radinator Sep 14 '16 at 08:47
  • Well, everything you just wrote is correct and confirms what I wrote: IDisposables ought to be Disposed. You wrote the opposite in your answer! – TaW Sep 14 '16 at 09:49
  • @TaW: ? What opposite? In my answer I wrote, that "you don't need to call `.Close()` or `.Dispose()`". The user/programmer does not need to write the call by himselfe. this taks is accomplished by the compiler. the compiler transformes the using directive into a try catch finally block where in the finally block the Dispose operation takes place. This is not needed to be written by the coder – Radinator Sep 14 '16 at 10:42
  • Well, I'm sure you meant the correct thing, but you wrote _'Since StreamReader and FileStream implement IDisposable, they will be freed automatically'_ which is at least misleading imo. They will be disposed of because of the `using` clauses not because `IDisposables` do it 'automatically'. – TaW Sep 14 '16 at 11:02