kristonpelz
kristonpelz

Reputation: 63

Getting path from a ExecutableFile.exe shortcuts

I have an (.exe) shortcut in a unknown folder (i.e. c:\dev) , pointing to my application.

I've been trying to obtain the shortcut path anytime my application is started by the shortcut.

I've tried different ways such as like Application.StartupPath , but it returns the path to the application executable file, and not the path to the shortcut.

Upvotes: 1

Views: 2584

Answers (3)

Johnson Manickam
Johnson Manickam

Reputation: 23

This code will help you. :)

namespace Shortcut
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using Shell32;

    class Program
    {
        public static string GetShortcutTargetFile(string shortcutFilename)
        {
            string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

            // This requires a COM Reference to Shell32 (Microsoft Shell Controls And Automation).
            Shell shell = new Shell();
            Folder folder = shell.NameSpace(pathOnly);
            FolderItem folderItem = folder.ParseName(filenameOnly);
            if (folderItem != null && folderItem.IsLink)
            {
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                return link.Path;
            }

            return string.Empty;
        }

        static void Main(string[] args)
        {
            const string path = @"C:\link to foobar.lnk";
            Console.WriteLine(GetShortcutTargetFile(path));
        }
    }
}

Upvotes: 1

Bimo
Bimo

Reputation: 6597

Here's a solution if you don't want to mess around with adding obsolete DLL's to your assembly:

private static string LnkToFile(string fileLink) {
    List<string> saveout = new List<string>();

    // KLUDGE Until M$ gets their $^%# together...
    string[] vbs_script = {
        "set WshShell = WScript.CreateObject(\"WScript.Shell\")\n",
        "set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))\n",
        "wscript.Echo Lnk.TargetPath\n"
    };

    string tempPath = System.IO.Path.GetTempPath();
    string tempFile = System.IO.Path.Combine(tempPath, "pathenator.vbs");

    File.WriteAllLines(tempFile, vbs_script);


    var scriptProc = new System.Diagnostics.Process();
    scriptProc.StartInfo.FileName               = @"cscript"; 
    scriptProc.StartInfo.Arguments              = " //nologo \"" 
                + tempFile + "\" \"" + fileLink + "\"";
    scriptProc.StartInfo.CreateNoWindow         = true;
    scriptProc.StartInfo.UseShellExecute        = false;
    scriptProc.StartInfo.RedirectStandardOutput = true;
    scriptProc.Start();
    scriptProc.WaitForExit();

    string lineall 
       = scriptProc.StandardOutput.ReadToEnd().Trim('\r', '\n', ' ', '\t');

    scriptProc.Close();

    return lineall;
}       

Upvotes: 0

Menelaos Vergis
Menelaos Vergis

Reputation: 3955

If you need to change the flow of your program from different shortcuts then pass an argument from each shortcut and read it in the main(args).

If you just need to get the shortcut folder then ensure that 'start in' text is blank and get the folder using

Environment.CurrentDirectory

Upvotes: 0

Related Questions