Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to get the current directory path but not of the application location but of it's shortcut location.

I tried these but they return the application's location.

Directory.GetCurrentDirectory();
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
like image 986
a1204773 Avatar asked Apr 02 '13 07:04

a1204773


1 Answers

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/

like image 125
aked Avatar answered Oct 30 '22 21:10

aked