c# shell32 code example

Example 1: shell32.dll c# example

//Read above for copy instructions
        private static void Copy(string startFile, string folderPath)
        {
            if (!File.Exists(startFile))
                throw new FileNotFoundException();

            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            Shell32.Shell objShell = new Shell32.Shell();
            Shell32.Folder destinationFolder = objShell.NameSpace(folderPath);
            Shell32.Folder sourceFile = objShell.NameSpace(startFile);
            foreach (var file in sourceFile.Items())
            {
                destinationFolder.CopyHere(file, 16);
            }
        }

Example 2: shell32.dll c# example

public static string GetLnkTarget(string lnkPath)
 {
     try
     {
         var shl = new Shell32.Shell();         // Move this to class scope
         var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath));
         var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath));
         var lnk = (Shell32.ShellLinkObject)itm.GetLink;
         return lnk.Target.Path;
     }
     catch (Exception)
     {
         return lnkPath;
     }
 }