Get the path of the shortcut used to launch the exe rather than the exe path itself

If a user launches my application from a shortcut how do I get the path of the shortcut that was used to execute the application? It's easy enough to get the directory of the application itself. All of the following provide useful information:

Open in new window

However, let's say that my executable is on a share, let's say the path is \\MyServer\D\SharedFolders\MyApplication\MyExe.exe. Now, let's say the user creates a shortcut in C:\Users\[username]\Documents\My Shortcuts\MyExe - Shortcut. If the user double clicks the shortcut all of the above will return the UNC path. Is there a way I can get the path of the actual shortcut that was used to launch the executable?
LVL 22
Russ SuterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul JacksonSoftware EngineerCommented:
I don't think this is possible, why would you need the shortcut's location?
Chinmay PatelChief Technical NinjaCommented:
Hi Russ,

I am not sure if I would want to spend time in C# to do this. I took an easy way out using Platform Invoke. Please read this link carefully:

Once you are done going through the above link,copy-paste the following code in your App

using System;
using System.Runtime.InteropServices;

namespace CrmXpress.EE.WinForm
    class EEHelper
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct STARTUPINFO
            public uint cb;
            public string lpReserved;
            public string lpDesktop;
            public string lpTitle;
            public uint dwX;
            public uint dwY;
            public uint dwXSize;
            public uint dwYSize;
            public uint dwXCountChars;
            public uint dwYCountChars;
            public uint dwFillAttribute;
            public uint dwFlags;
            public ushort wShowWindow;
            public ushort cbReserved2;
            public IntPtr lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;

        [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetStartupInfoA")]
        public static extern void GetStartupInfo(out STARTUPINFO lpStartupInfo);

Open in new window

And in your MainForm Constructor/s
public frmMain()
            EEHelper.STARTUPINFO startupInfo;
            EEHelper.GetStartupInfo(out startupInfo);

// I did not test this but I think it doesn't matter where do you put this code, it will give you the desired results.

Open in new window

As this is a PInvoke call I suggest you handle the error appropriately and test it in various situations which you see your end users in before putting this code in a finalize product. It is not that GetStartupInfo is a rogue/risky call but it is always wise to be cautions while making PInvoke calls. Let me know if you have any questions/concerns.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russ SuterAuthor Commented:
Well, I may not like the answer but that is, indeed, the answer. It's just not possible. I'll find another way.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Chinmay PatelChief Technical NinjaCommented:
Did you try the solution I have posted?
Russ SuterAuthor Commented:
Sorry, I didn't see it. It actually worked perfectly. Thanks! :)
Chinmay PatelChief Technical NinjaCommented:
Dude... you gave me a little heart attack... :P.. Glad it worked for you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.