How to copy a file programmatically in asp.net c#

Copy a file programmatically
.Net framework File.Copy(String, String) method allow us to copy an existing file to a new file. overwriting a file of the same name is not allowed in this overloaded method. File class Copy() method exists in System.IO namespace.

Copy(String, String) method has two required parameters named 'sourceFileName' and 'destFileName'. both parameters value data type is System.String. 'sourceFileName' parameter represents the file to copy. 'destFileName' parameter represents the name of the destination file. this parameter value cannot be a directory or an existing file.

File.Copy(String, String) method throw UnauthorizedAccessException exception, if the caller does not have the required permission. method throw ArgumentNullException, if the 'sourceFileName' or 'destFileName' is null. it throw FileNotFoundException, if the 'sourceFileName' was not found. method also throw ArgumentException, PathTooLongException, DirectoryNotFoundException, IOException and NotSupportedException.

.Net framework File.Copy(String, String, Boolean) overloaded method allow us to copy an existing file to a new file and overwriting a file of the same name is allowed.

the following asp.net c# example code demonstrate us how can we copy a file programmatically at run time in an asp.net application.
FileCopy.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if (!this.IsPostBack) 
        {
            string file = Request.PhysicalApplicationPath + "parrot.jpg";
            string fileDes = Request.PhysicalApplicationPath + "Images\\parrot.jpg";
            TextBox1.Text = file;
            TextBox2.Text = fileDes;
        }
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        try
        {
            File.Copy(TextBox1.Text,TextBox2.Text);
            Label1.Text = "File copied";
        }
        catch(Exception ex)
        {
            Label1.Text = "an error occured!<br/>"+ ex.ToString();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to copy file in asp.net programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">asp.net example: file copy</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Larger" 
            ForeColor="Crimson"
            >
        </asp:Label>
        <br /><br />
        <asp:Label 
            ID="Label2" 
            runat="server" 
            Text="Source File"
            ForeColor="OrangeRed"
            Font-Bold="true"
            >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1" 
             runat="server" 
             ReadOnly="true"
             BackColor="OrangeRed"
             ForeColor="AliceBlue"
             >
        </asp:TextBox>
        <br />
        <asp:Label 
            ID="Label3" 
            runat="server" 
            Text="Destination File"
            ForeColor="OrangeRed"
            Font-Bold="true"
            >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox2" 
             runat="server" 
             ReadOnly="true"
             BackColor="OrangeRed"
             ForeColor="AliceBlue"
             Width="250"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Font-Bold="true" 
            ForeColor="IndianRed"
            Text="Copy File"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples