Monday, 20 July 2015

How To Copy File From One Folder (Source) To Another (Target) in Asp.Net Using C#.Net

7/20/2015 - By Pranav Singh 2

This article will show you how you can copy file from one folder (source) to another (target) in asp.net using c#.net.




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.

Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top