Simply Copy the file and Move to destination folder in C# application

Here I explained C# program about Simply Copy the file and Move to destination folder. Using IO namespace.then FileInfo method is used to corresponding file location.File.Exists type is used to find file already exists or not.CopyTo method is used to copy the file from located position to corresponding folder.

Copy and Move the File in corresponding Location

Namespace Used:

using System.Data.SqlClient;
using System.IO;

Using some Method:

  • FileInfo

  • Example:FileInfo f = new FileInfo(@"c:\\test.txt");
  • File.Exists()(

  • Example:File.Exists(@"C:\\temp\\" + f.Name)
  • File.Exists()

  • Example:File.Exists(strNewFileNmae)
  • CopyTo()

  • Example:f.CopyTo(@"C:\\temp\\" + f.Name);

    Complete C# Code:


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using System.IO;

    public partial class FileStore : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
    //DirectoryInfo info = Directory.CreateDirectory(@"c:\\test1");
    //string filepath = "d:\\test1";
    //FileStream fle = File.Create(filepath,1024,FileOptions.WriteThrough);
    FileInfo f = new FileInfo(@"c:\\test.txt");
    if (File.Exists(@"C:\\temp\\" + f.Name))
    string strOldFile = f.Name;
    string strNewFileNmae = @"C:\\temp\\" +
    strOldFile.Substring(0, (strOldFile.IndexOf(".") - 1)) +
    DateTime.Now.ToString("mm-dd-yyyy") + ".txt";
    if (File.Exists(strNewFileNmae))
    Label1.Text = "File already exists";
    Label2.Visible = false;
    f.CopyTo(strNewFileNmae, false);
    f.CopyTo(@"C:\\temp\\" + f.Name);
    Label1.Text = "File has been created successfully..";
    Label2.Visible = true;
    Label2.Text = "Find Location is" + "C:\\temp\\" + info + ".txt";



    File has been copied from c:\test.txt to c:\temp\test.txt folder


    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: