c# - How to write a StringBuilder contents to a file

c# example - stringbuilder to file
stringbuilder-to-file.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.IO" %>
  
<!DOCTYPE html>
  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        StringBuilder stringb = new StringBuilder();
        stringb.Append("Eclectus Parrot.");
        stringb.AppendLine();

        stringb.Append("Hyacinth Macaw.");
        stringb.AppendLine();

        stringb.Append("Vulturine Parrot.");
        stringb.AppendLine();

        stringb.Append("Cuban Amazon.");

        Label1.Text = stringb.ToString();

        string appPath = Request.PhysicalApplicationPath;
        string filePath = appPath + "stringbuilderoutput.txt";  

        //this code section write stringbuilder content to physical text file.
        using (StreamWriter swriter = new StreamWriter(filePath))
        {
            swriter.Write(stringb.ToString());
        }        
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - stringbuilder to file</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - stringbuilder to file
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <br />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br /><br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="stringbuilder to file"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>

More c# examples