File copy and overwrite
.NET Framework File class’s Copy(String, String, Boolean) overloaded
method allow us to copy an existing file to a new file. Overwriting a file of
the same name is allowed. File class’s Copy() method exists in the System.IO
namespace.
The Copy method requires passing three parameters named sourceFileName, destFileName, and overwrite. The sourceFileName parameter value data type is a String which represents the file to copy. The destFileName parameter value data type is also a String which represents the name of the destination file. This parameter cannot be a directory.
The overwrite parameter value data type is a Boolean. this parameter value true indicates the destination file can be overwritten. If we don't want to allow destination file overwrite then we need to set this parameter value to false.
The Copy() method throws an UnauthorizedAccessException exception if the caller does not have the required permission or destFileName is read-only. Copy() method throws an ArgumentNullException exception if the sourceFileName or the destFileName is null.
This method throws an IOException if destFileName is exists and overwrite is false or an I/O error has occurred. The method throws a NotSupportedException if sourceFileName or destFileName is in an invalid format.
Copy() method also throws ArgumentException, PathTooLongException, DirectoryNotFoundException, and FileNotFoundException.
The following ASP.NET C# example code demonstrates to us how can we copy a file and overwrite programmatically at run time in an ASP.NET application.
The Copy method requires passing three parameters named sourceFileName, destFileName, and overwrite. The sourceFileName parameter value data type is a String which represents the file to copy. The destFileName parameter value data type is also a String which represents the name of the destination file. This parameter cannot be a directory.
The overwrite parameter value data type is a Boolean. this parameter value true indicates the destination file can be overwritten. If we don't want to allow destination file overwrite then we need to set this parameter value to false.
The Copy() method throws an UnauthorizedAccessException exception if the caller does not have the required permission or destFileName is read-only. Copy() method throws an ArgumentNullException exception if the sourceFileName or the destFileName is null.
This method throws an IOException if destFileName is exists and overwrite is false or an I/O error has occurred. The method throws a NotSupportedException if sourceFileName or destFileName is in an invalid format.
Copy() method also throws ArgumentException, PathTooLongException, DirectoryNotFoundException, and FileNotFoundException.
The following ASP.NET C# example code demonstrates to us how can we copy a file and overwrite programmatically at run time in an ASP.NET application.
FileCopyOverWrite.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
if (!this.IsPostBack)
{
TextBox1.Text = Request.PhysicalApplicationPath + "HP2133.jpg";
TextBox2.Text = Request.PhysicalApplicationPath + "Images\\HP2133.jpg";
}
}
protected void Button1_Click(object sender, System.EventArgs e) {
try
{
File.Copy(TextBox1.Text,TextBox2.Text,true);
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 and over write in asp.net programmatically</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Green">asp.net example: file copy and over write</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Larger"
ForeColor="DodgerBlue"
>
</asp:Label>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
Text="Source File"
ForeColor="SeaGreen"
Font-Bold="true"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
ReadOnly="true"
BackColor="SeaGreen"
ForeColor="White"
>
</asp:TextBox>
<br />
<asp:Label
ID="Label3"
runat="server"
Text="Destination File"
ForeColor="SeaGreen"
Font-Bold="true"
>
</asp:Label>
<asp:TextBox
ID="TextBox2"
runat="server"
ReadOnly="true"
BackColor="SeaGreen"
ForeColor="White"
Width="250"
>
</asp:TextBox>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="SeaGreen"
Text="Copy File"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>


