如何知道* .lnk文件是否链接到某个东西

时间:2013-02-10 11:46:58

标签: visual-studio-2010 c#-4.0

如何知道* .lnk文件是否链接到某个东西。我怎样才能在C#中实现这一目标?

1 个答案:

答案 0 :(得分:0)

  1. 获取Link Target Getting specific file attributes
  2. 使用File.Exists
  3. 检查Link Target Path中的文件(如果存在)
    class Program
    {
        [STAThread]
        static void Main()
        {
            List<string> arrHeaders = new List<string>();
    
            Shell32.Shell shell = new Shell32.Shell();
            Shell32.Folder objFolder;
    
            objFolder = shell.NameSpace(@"D:\shortcuts");
    
            for (int i = 0; i < short.MaxValue; i++)
            {
                string header = objFolder.GetDetailsOf(null, i);
                if (String.IsNullOrEmpty(header))
                    break;
                arrHeaders.Add(header);
            }
    
            foreach (Shell32.FolderItem2 item in objFolder.Items())
            {
                for (int i = 0; i < arrHeaders.Count; i++)
                {
                    //Console.WriteLine("{0}\t{1}: {2}", i, arrHeaders[i], objFolder.GetDetailsOf(item, i));
                    if (arrHeaders[i] == "Link target")
                    {
                        var getPath = objFolder.GetDetailsOf(item, i);
                        Console.WriteLine("{0}", getPath);
                        if (File.Exists(getPath))
                        {
                            Console.WriteLine("The file exists.");
                        }
                        else
                        {
                            Console.WriteLine("File not exist");
                            //Do stuff to delete
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }