Copying or Moving a File (C#) : FileInfo « File Directory « ASP.Net






Copying or Moving a File (C#)


<%@ Page Language="C#" %>
<script runat="server">
void CopyFile_Click(object sender, System.EventArgs e)
{
  try
  {
    string sourceFile = Server.MapPath("Data.txt");
    string destinationFile = Server.MapPath("Datacopy.txt");
    System.IO.File.Copy(sourceFile, destinationFile);
  }
  catch (Exception ex)
  {
    MessageLabel.Text = ex.Message;
  }
}

void MoveFile_Click(object sender, System.EventArgs e)
{
  try
  {
    string sourceFile = Server.MapPath("Data.txt");
    string destinationFile = Server.MapPath("Datamove.txt");
    System.IO.File.Move(sourceFile, destinationFile);
  }
  catch (Exception ex)
  {
    MessageLabel.Text = ex.Message;
  }
}
</script>
<html>
  <head>
    <title>Copying or Moving a File</title>
  </head>
  <body>
    <form id="MainForm" runat="server">
      Copy "Data.txt" to "Datacopy.txt"
      <asp:button id="CopyFile" runat="server" text="Copy File" onclick="CopyFile_Click" />
      <br />
      Move "Data.txt" to "Datamove.txt"
      <asp:button id="MoveFile" runat="server" text="Move File" onclick="MoveFile_Click" />
      <br />
      <asp:Label ID="MessageLabel" Runat="server" />
    </form>
  </body>
</html>

 








Related examples in the same category

1.Inquiring Information About a File (C#)
2.Inquiring Information About a File (VB)
3.Copying or Moving a File (VB)