Learn when you want, where you want with convenient online training courses. Sign up now!
Experts Exchange Solution brought to you by
"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
string fileName = "MyFile.txt";
string sourceFile = @"C:\Users\Public\TestFolder";
string targetFile= @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourceFile, fileName);
string destFile = System.IO.Path.Combine(targetFile, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
Open in new window
string searchPath = "folder\\to\\search\\for\\file";
string lnkToCopy = "shortcut.lnk";
string lnkCopyTo = "folder\\to\\copy\\to";
IEnumerable<string> collection = System.IO.Directory.EnumerateFiles(searchPath, "*.lnk", System.IO.SearchOption.TopDirectoryOnly); //create an ennumerable list of all the files in the directory
foreach (string f in collection)
System.IO.FileInfo fi = new System.IO.FileInfo(f);
if (fi.Name == lnkToCopy) //check if the file name matches the file we want to copy
System.IO.File.Copy(searchPath+"\\"+lnkToCopy, lnkCopyTo+"\\"+lnkToCopy); //copy the file
Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.