Tuesday, February 22, 2011

To create text file and write text programmatically





StreamWriter.aspx

<%@ Page Language="C#" %>  
<%@ Import Namespace="System.IO" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
        string appPath = Request.PhysicalApplicationPath;  
        string filePath = appPath + "Text.txt";  
        StreamWriter w;  
        w = File.CreateText(filePath);  
        w.WriteLine("This is a test line.");  
        w.WriteLine("This is another line.");  
        w.Flush();  
        w.Close();  
        Label1.Text = "File created and write successfully!<br />";  
        Label1.Text += filePath;  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>How to create text file and write text programmatically in asp.net</title>  
</head>   
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Red">asp.net StreamWriter example:<br />Create File and Write In It</h2>  
        <asp:Label   
             ID="Label1"   
             runat="server"   
             Font-Size="Large"  
             ForeColor="SeaGreen"  
             Font-Bold="true"  
             Font-Italic="true"  
             >  
        </asp:Label>  
        <br /><br />  
        <asp:Button   
             ID="Button1"   
             runat="server"   
             OnClick="Button1_Click"  
             Font-Bold="true"  
             Text="Create File and Write Text"  
             ForeColor="SeaGreen"  
             />     
    </div>  
    </form>  
</body>  
</html>


No comments: