Skip to content

Instantly share code, notes, and snippets.

@sin2akshay
Created April 24, 2018 11:18
Show Gist options
  • Save sin2akshay/dcf2393273f385f0703a9247b1c308cb to your computer and use it in GitHub Desktop.
Save sin2akshay/dcf2393273f385f0703a9247b1c308cb to your computer and use it in GitHub Desktop.
C# - Transfer file from one Folder to other
/// <summary>
/// Transfer file from one Folder to other
/// </summary>
/// <param name="path">Path to Target Folder</param>
/// <param name="FileName">Full Path to Source File</param>
public void TransFileToSpecifiedFolder(string path, string FileName)
{
//Deleting file from destination folder if it exists, else exception occurs
string path2 = path + "\\" + Path.GetFileName(FileName);
try
{
LogManager.EventLog("Inside TransFileToSpecifiedFolder: Moving '" + Path.GetFileName(FileName) + "' to '" + path + "'");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (File.Exists(path2))
File.Delete(path2);
File.Move(FileName, path2);
}
catch (Exception ex)
{
//Log the error to your logger
LogManager.EventLog("[Error] Inside TransFileToSpecifiedFolder: " + ex.Message);
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment