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






<%@ Page Language="c#" Explicit="true" Strict="true" %>
<%@ Import Namespace = "System.IO" %>
<script language="c#" runat="Server">
  public void ReadFile(object sender, EventArgs e)
  {
    StreamReader FileStreamReader = null;
    try
    {
      FileStreamReader = File.OpenText (Server.MapPath(".\\Upload\\") + "test.txt");
      TextContent.Text = FileStreamReader.ReadToEnd();
      FileStreamReader.Close();
      OutputLabel.Text = "File Succesfully Read!";
    }
    catch (Exception exc)
    {
      OutputLabel.Text = "Error in Reading file. Error is " + exc.ToString();
    }
    finally
    {
      if(FileStreamReader!=null)
        FileStreamReader.Close();
    }
  }
</script>
<html>
  <head>
    <title>Opening a File</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:TextBox ID="TextContent" Runat="server" TextMode="MultiLine" Rows="10" Columns="75"></asp:TextBox><br>
      <asp:Button ID="Submit" Runat="server" Text="Submit" OnClick="ReadFile"></asp:Button>
      <asp:Label ID="OutputLabel" 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