# finding and copying .lnk files

I have a windows application that needs to copy a .lnk file from one place to another, however I get the error that the file cannot be found;  the name of the file is myFile.lnk.

the folder permissions are open to everybody.  what is it I'm missing?
###### Who is Participating?
I wear a lot of hats...

"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.

Commented:
The following works for me :

 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.
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);


Let me know if it works for you.
0
Commented:
Any errors being thrown?
0
Commented:
I just wrote and tested this 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
break;
}
}

0

Experts Exchange Solution brought to you by