Web Dev
Web Dev

Reputation: 735

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.

Upvotes: 7

Views: 526

Answers (1)

Jeffrey Zhang
Jeffrey Zhang

Reputation: 438

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");
}

Upvotes: 4

Related Questions