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

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » File Directory » FileInfo 
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)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.