andrew4582

ShortcutHelper

Dec 20th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1.     using System.IO;
  2.  
  3.     internal static class ShortcutHelper
  4.     {
  5.         internal static bool IsShortcut(string path)
  6.         {
  7.             if (!File.Exists(path))
  8.             {
  9.                 return false;
  10.             }
  11.  
  12.             string directory = Path.GetDirectoryName(path);
  13.             string file = Path.GetFileName(path);
  14.  
  15.             Shell32.Shell shell = new Shell32.Shell();
  16.             Shell32.Folder folder = shell.NameSpace(directory);
  17.             Shell32.FolderItem folderItem = folder.ParseName(file);
  18.  
  19.             if (folderItem != null)
  20.             {
  21.                 return folderItem.IsLink;
  22.             }
  23.  
  24.             return false;
  25.         }
  26.  
  27.         internal static string ResolveShortcut(string path)
  28.         {
  29.             if (IsShortcut(path))
  30.             {
  31.                 string directory = Path.GetDirectoryName(path);
  32.                 string file = Path.GetFileName(path);
  33.  
  34.                 Shell32.Shell shell = new Shell32.Shell();
  35.                 Shell32.Folder folder = shell.NameSpace(directory);
  36.                 Shell32.FolderItem folderItem = folder.ParseName(file);
  37.  
  38.                 Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  39.                 return link.Path;
  40.             }
  41.             return string.Empty;
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment