首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Write to a file C# ASP.net on按钮点击

Write to a file C# ASP.net on按钮点击
EN

Stack Overflow用户
提问于 2018-07-03 01:55:26
回答 1查看 1.1K关注 0票数 1

我无法让此asp.net页写入文件。我一直在关注这个guide。我不确定我做错了什么,因为我可以使用此按钮更改标签文本,但不能将"Hello World“打印到同一目录下的文本文件中。

代码语言:javascript
复制
<%@ Page Language="C#" %>
<script runat="server">
void Button1_Click(Object sender, EventArgs e) 
{ 
        using (StreamWriter w = new StreamWriter(Server.MapPath("~/Saved Layouts.txt"), true))
        {
            w.WriteLine("Hello World"); // Write the text
        }
}
</script>
<html>
<head>
  <title>Single-File Page Model</title>
</head>
<body>
  <form runat="server">
    <div>
       <asp:Label id="Label1" 
         runat="server" Text="Label">
       </asp:Label>
       <br />
       <asp:Button id="Button1" 
         runat="server" 
         onclick="Button1_Click" 
         Text="Button">
      </asp:Button>
    </div>
  </form>
</body>
</html>

EN

回答 1

Stack Overflow用户

发布于 2018-07-03 20:33:44

问题可能是这个错误“类型或名称空间名称'StreamWriter‘找不到”。这是因为不存在using System.IO。要解决此问题,请写出StreamWriter的完整NameSpace。

代码语言:javascript
复制
using (System.IO.StreamWriter w = new System.IO.StreamWriter(Server.MapPath("~/Saved Layouts.txt"), true))
{
    w.WriteLine("Hello World"); // Write the text
}

或将其添加到单个页面

代码语言:javascript
复制
<%@ Import Namespace="System.IO" %>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51141428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档