IO Close () file input error in ASP.NET MVC 6

I am making a simple IO file in MVC6. I added the System.IO NuGet package. However, this gives me a compile-time error. VS IDE does not show a red mark when entering 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(); 

Mistake

StreamWriter does not contain a definition for "Close", and the "Close" extension method cannot be found that accepts the first argument of the type "StreamWriter" (do you miss the using directive or assembly references?)

Thanks.

+7
asp.net-mvc asp.net-core-mvc
source share
1 answer

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

 using (var writer = System.IO.File.CreateText("your_path")) { writer.WriteLine("text"); } 
+4
source share

All Articles