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.


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");
}
链接地址: http://www.djcxy.com/p/86016.html

上一篇: 在自定义textarea中显示插入符号而不显示其文本

下一篇: ASP.NET MVC 6中的文件IO Close()方法错误