加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET MVC 6中的文件IO Close()方法错误

发布时间:2020-12-16 04:05:54 所属栏目:asp.Net 来源:网络整理
导读:我在MVC6中做了一个简单的文件IO.我添加了System.IO NuGet包.但是,它给了我编译时错误.键入代码时,VS IDE不显示任何红色标记. Close()方法也出现在intellisense中.请帮忙! 我的代码 StreamWriter writer = System.IO.File.CreateText("some_valid_path");wr
我在MVC6中做了一个简单的文件IO.我添加了System.IO NuGet包.但是,它给了我编译时错误.键入代码时,VS IDE不显示任何红色标记. Close()方法也出现在intellisense中.请帮忙!

我的代码

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

错误

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?)

谢谢.

解决方法

你使用核心CLR吗? StreamWriter.Close方法在核心CLR中不可用.您可以使用Dispose方法替换.
您还可以使用using语句:
using (var writer = System.IO.File.CreateText("your_path"))
{
    writer.WriteLine("text");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读