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






<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" %>
<script runat="server">

    void Page_Load(Object Source, EventArgs E)
    {
        MessageLiteral.Text = "";
    }
    
    void DeleteFile(Object Source, EventArgs E)
    {
        FileInfo fileSource = new FileInfo(FileNameTextBox.Text);
    
      try{
          if (fileSource.Exists){
              fileSource.Delete();
              MessageLiteral.Text = "File has been deleted.";
        }
          else
              MessageLiteral.Text = FileNameTextBox.Text + " does not exist.";
      }
      catch (Exception _Error){
        MessageLiteral.Text = _Error.Message;
      }
    }

</script>
<html>
  <head>
    <title>Deleting a File</title>
  </head>
  <body>
    <form runat="server">
      <h4>Type Path/Name of file to delete it (disabled for live demo)
      </h4>
      
        <asp:TextBox id="FileNameTextBox" runat="server"></asp:TextBox>
        &nbsp;<asp:Button id="DeleteFileButton" onclick="DeleteFile" runat="server" Text="Delete File"></asp:Button>
      
      
        <asp:Literal id="MessageLiteral" runat="server"></asp:Literal>
      
    </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