Creating a File (C#) : File « File Directory « ASP.NET Tutorial






<%@ 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>








10.1.File
10.1.1.Get file information (VB.net)
10.1.2.Opening a File (C#)
10.1.3.Opening a File (VB)
10.1.4.Delete a file (C#)
10.1.5.Delete a file (VB)
10.1.6.Creating a File (C#)
10.1.7.Creating a File (VB)
10.1.8.Load file content to DropDownList (C#)
10.1.9.Load file content to DropDownList (VB)
10.1.10.Rename File