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:
Example:FileInfo f = new FileInfo(@"c:\\test.txt");
Example:File.Exists(@"C:\\temp\\" + f.Name)
Example:File.Exists(strNewFileNmae)
Example:f.CopyTo(@"C:\\temp\\" + f.Name);Complete C# Code:
Filename:xxx.aspx.cs
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;
return;
}
else
{
f.CopyTo(strNewFileNmae, false);
}
}
else
{
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";
}
}Output
File has been copied from c:\test.txt to c:\temp\test.txt folder
FileName:tes43-21-2011.txt