Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File IO Close() method error in ASP.NET MVC 6

I am doing a simple file IO in MVC6. I have added System.IO NuGet package. However, it gives me compile time error. VS IDE doesn't show any red mark when I type the code. The Close() method also appears in intellisense. Please help!

My Code

StreamWriter writer = System.IO.File.CreateText("some_valid_path");
writer.WriteLine("test");
writer.Close();

Error

StreamWriter does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'StreamWriter' could be found (are you missing a using directive or an assembly reference?)

Thank you.

like image 861
Web Dev Avatar asked Jun 22 '15 09:06

Web Dev


1 Answers

Do you use the core CLR? The StreamWriter.Close method are not available in core CLR. You can use Dispose method replace. You also can use using statement:

using (var writer = System.IO.File.CreateText("your_path"))
{
    writer.WriteLine("text");
}
like image 62
Jeffrey Zhang Avatar answered Sep 20 '22 10:09

Jeffrey Zhang