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?
bearsomgConnect With a Mentor 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

Open in new window

unknown_routineConnect With a Mentor 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))

        // 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

Let me know if it works for you.
Please post your code.
Any errors being thrown?
dhenderson12Author Commented:
thanks folks, but the error went away ?!?  i must have screwed something up when i was renaming files....
Question has a verified solution.

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.

All Courses

From novice to tech pro — start learning today.