System.IO.Path.Combine to merge File Path

System.IO.Path.Combine to merget file path in c#

Just come across an interesting method System.IO.Path.Combine.  Thought of sharing with my blog readers. This method is used to merge the path of the file with another string.

string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.

string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and 
// overwrite the destination file if it already exists.


System.IO.File.Copy(sourceFile, destFile, true);

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
http://www.kaashivinfotech.com/