<%@ Page Language="c#" %>
<%@ Import Namespace = "System.IO" %>
<script language="c#" runat="Server">
public void CreateFile(object sender, EventArgs e)
{
StreamWriter myFileStream = null;
if(TextContent.Text.Trim().Length > 0)
{
try
{
myFileStream = File.CreateText(Server.MapPath(".\\Upload\\") + "test.txt");
myFileStream.WriteLine(TextContent.Text);
myFileStream.Close();
Output.Text = "File Successfully Created!";
}
catch (Exception exc)
{
Output.Text = "Error in Creating file. Error is " + exc.ToString();
}
finally
{
if(myFileStream != null)
myFileStream.Close();
}
}
else
{
Output.Text = "File not created, because you didn't enter anything!";
}
}
</script>
<HTML>
<HEAD>
<title>Creating a File</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label ID="Msg" Runat="server" Text="Please enter some text: "></asp:Label><br>
<asp:TextBox ID="TextContent" Runat="server" TextMode="MultiLine" Rows="10" Columns="75"></asp:TextBox><br>
<asp:Button ID="Submit" Runat="server" Text="Submit" OnClick="CreateFile"></asp:Button>
<asp:Label ID="Output" Runat="server"></asp:Label>
</form>
</body>
</HTML>
|