File copy and overwrite
.Net framework File.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 Copy() method exists in System.IO namespace.
the Copy method require to pass three parameters named 'sourceFileName', 'destFileName' and 'overwrite'. the 'sourceFileName' parameter value data type is System.String which represents the file to copy. 'destFileName' parameter value data type also System.String which represents the name of the destination file. this parameter cannot be a directory.
'overwrite' parameter value data type is System.Boolean. this parameter value 'true' indicate 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 throw UnauthorizedAccessException, if the caller does not have the required permission or 'destFileName' is read-only. Copy() method throw ArgumentNullException exception, if the 'sourceFileName' or the 'destFileName' is null.
method throw IOException, if 'destFileName' is exists and overwrite is false or an I/O error has occurred. method throw NotSupportedException, if 'sourceFileName' or 'destFileName' is in an invalid format.
Copy() method also throw ArgumentException, PathTooLongException, DirectoryNotFoundException and FileNotFoundException.
the following asp.net c# example code demonstrate us how can we copy a file and overwrite programmatically at run time in an asp.net application.
the Copy method require to pass three parameters named 'sourceFileName', 'destFileName' and 'overwrite'. the 'sourceFileName' parameter value data type is System.String which represents the file to copy. 'destFileName' parameter value data type also System.String which represents the name of the destination file. this parameter cannot be a directory.
'overwrite' parameter value data type is System.Boolean. this parameter value 'true' indicate 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 throw UnauthorizedAccessException, if the caller does not have the required permission or 'destFileName' is read-only. Copy() method throw ArgumentNullException exception, if the 'sourceFileName' or the 'destFileName' is null.
method throw IOException, if 'destFileName' is exists and overwrite is false or an I/O error has occurred. method throw NotSupportedException, if 'sourceFileName' or 'destFileName' is in an invalid format.
Copy() method also throw ArgumentException, PathTooLongException, DirectoryNotFoundException and FileNotFoundException.
the following asp.net c# example code demonstrate 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>