How to get the current directory path of application's shortcut

If adding a COM Object reference is not a problem , Add COM Object Reference - Windows Script Host Object Model

i ran this code in my desktop folder and it did work. for current folder use - Environment.CurrentDirectory

using System;
using System.IO;
using IWshRuntimeLibrary;  //COM object -Windows Script Host Object Model

namespace csCon
{
    class Program
    {
        static void Main(string[] args)
        {
            // Folder is set to Desktop 
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var di = new DirectoryInfo(dir);
            FileInfo[] fis = di.GetFiles();
            if (fis.Length > 0)
            {
                foreach (FileInfo fi in fis)
                {
                    if (fi.FullName.EndsWith("lnk"))
                    {
                        IWshShell shell = new WshShell();
                        var lnk = shell.CreateShortcut(fi.FullName) as IWshShortcut;
                        if (lnk != null)
                        {
                            Console.WriteLine("Link name: {0}", lnk.FullName);
                            Console.WriteLine("link target: {0}", lnk.TargetPath);
                            Console.WriteLine("link working: {0}", lnk.WorkingDirectory);
                            Console.WriteLine("description: {0}", lnk.Description);
                        }

                    }
                }
            }
        }
    }
}

enter image description here

Code Reference from Forum : http://www.neowin.net/forum/topic/658928-c%23-resolve-lnk-files/


According to the process API reference in MSDN, the process STARTUPINFO struct for a given process contains the information about the shortcut .lnk file in the title member. There is a flag present in the dwFlags struct member that is set when this is the case - so it appears that this is not always set (im guessing if you ran the exe directly)

From MSDN:

STARTF_TITLEISLINKNAME: 0x00000800

The lpTitle member contains the path of the shortcut file (.lnk) that the user invoked to start this process. This is typically set by the shell when a .lnk file pointing to the launched application is invoked. Most applications will not need to set this value. This flag cannot be used with STARTF_TITLEISAPPID.

Reference here.