Experts Exchange connects you with the people and services you need so you can get back to work.
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
Open in new window
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);
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.
Premium members get this course for $79.20.
Premium members get this course for $24.99.
Premium members get this course for $159.20.
Premium members get this course for $37.50.
Premium members get this course for $148.75.
Premium members get this course for $12.50.
Premium members get this course for $143.20.