Reputation: 307
I can't get this asp.net page do write to a file. I was following this guide. I am not sure what i am doing wrong because i can use this button to change label text, but not to print "Hello World" into a text file in the same directory.
<%@ 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>
Upvotes: 1
Views: 1525
Reputation: 35514
The problem is probably this error "The type or namespace name 'StreamWriter' could not be found". That's because there is no using System.IO
present. To fix this write out the complete NameSpace of the StreamWriter.
using (System.IO.StreamWriter w = new System.IO.StreamWriter(Server.MapPath("~/Saved Layouts.txt"), true))
{
w.WriteLine("Hello World"); // Write the text
}
Or add it to the single page
<%@ Import Namespace="System.IO" %>
Upvotes: 1