Sunday, July 3, 2011

Copy File from One location to Another location with same file name

Example: I have the file Path for input file "C:/Documents/Sample.txt" now I want to use this path to get the File and then Save it to a Different Location with same file name like "E:/Downloads/Sample.txt".
Answer:
string sourcePath = @"C:\Documents\Sample.txt";
string fileName = System.IO.Path.GetFileName(sourcePath);
string destinationPath = System.IO.Path.Combine (@"E:\Downloads", fileName);  
// will result in E:\Downloads\Sample.txt
System.IO.File.Copy(sourcePath,destinationPath,true);  eg: Path: "c:\\sampletest.jpg"
                     true use for- if file already exists in destination path then it overwrites.



No comments:

Post a Comment