0

How to know if a *.lnk file link to something or not. How can I achieve that in C#?

  • Perhaps check the file exists on the link property path? – ericosg Feb 10 '13 at 11:48
  • http://stackoverflow.com/questions/139010/how-to-resolve-a-lnk-in-c-sharp and then use the appropriate file.exists to see if it exists. – ericosg Feb 10 '13 at 11:49
  • A link always links to something, do you mean that you want to check if that something is currently reachable? – Guffa Feb 10 '13 at 11:55
  • I want to know if a lnk file link to something or not. If not, I want to delete them. –  Feb 10 '13 at 11:57

1 Answers1

0
  1. Get the Link Target Getting specific file attributes
  2. Check the file from Link Target Path if exist using the File.Exists
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();
    }
}
Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44