This article will show you how you can copy file from one
folder (source) to another (target) in asp.net using c#.net.
Some of my previous articles are as follows: How
to Read Text File Each Line Using c# In Asp.net, Cookies
Create, Read and Delete Operation Using jQuery in Asp.Net, Css
Class Change of Button On Radiobutton Selection In Asp.Net Using jQuery, Shaking
Login Box Open Button Click Using jQuery In Asp.Net, Asp.Net
Ajax MaskedEdit With Validation In Asp.Net Using C#, jQuery
Error Message Display Using Popup Dialog Box In Asp.Net, Confirmation
Message With Yes, No Button In Asp.Net Using ConfirmButtonExtender and
ModalPopupExtender, Validate
DropDownlist Using RegularExpression In Asp.Net.
So for this article first we will create a tow folder one for source and other for target.
So for this article first we will create a tow folder one for source and other for target.
Now we will create the asp.net application and add the below
code for the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm18.aspx.cs" Inherits="WebApplication2.WebForm18"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How To
Copy File From One Folder(Source) To Another(Target) in Asp.Net Using C#.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Make Copy" OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="lblMessage" runat="server" Text="" Style="color: #FF0000"></asp:Label>
</div>
</form>
</body>
</html>
|
Now on button click on the control we will put the code as shown
below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication2
{
public partial class WebForm18 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
string
fileName = "TextFile1.txt";
string
targetFolder = "";
if
(Directory.Exists(Server.MapPath(targetFolder)))
{
string
sourceFilePath = @"Source\" + fileName;
string
targerFilePath = @"Target\" +
fileName;
//copy
file from one folder to another folder
//True
- it means overright of file is allowed.
File.Copy(Server.MapPath(sourceFilePath),
Server.MapPath(targerFilePath), true);
lblMessage.Text = "File copy successfull.";
}
else
{
lblMessage.Text = "Target folder does not exists";
}
}
}
}
|
In above code check the below shown line.
File.Copy(Server.MapPath(sourceFilePath), Server.MapPath(targerFilePath), true);
|
The above code is used for making copy of the source file to the target folder. Now we have done run the application to check the output.
Good Post.
ReplyDeleteGood Post.
ReplyDelete