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?
dhenderson12Asked:
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.

unknown_routineCommented:
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);

Open in new window


Let me know if it works for you.
0
UnifiedISCommented:
Please post your code.
Any errors being thrown?
0
bearsomgCommented:
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;
                }
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhenderson12Author Commented:
thanks folks, but the error went away ?!?  i must have screwed something up when i was renaming files....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.