c# - How to create a csv from a string

String to csv
string-to-csv.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.IO" %>
  
<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        string birds = "Sandpiper Plover Tern Skua Puffin";
        string result = birds.Replace(' ', ',');

        Label1.Text = "string of birds............<br />";
        Label1.Text += birds;

        Label1.Text += "<br /><br />string to comma separated list............<br />";
        Label1.Text += result;

        string appPath = Request.PhysicalApplicationPath;
        string filePath = appPath + "birds.csv";

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