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

Russ Suter
Russ Suter used Ask the Experts™
on
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:
Directory.GetCurrentDirectory();
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);

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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
I don't think this is possible, why would you need the shortcut's location?
Chief Technology Ninja
Distinguished Expert 2018
Commented:
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: https://www.pinvoke.net/default.aspx/kernel32/GetStartupInfo.html

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);
            MessageBox.Show(startupInfo.lpTitle);

// 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.
            InitializeComponent();
        }

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.

Regards,
Chinmay.
Russ SuterSenior Software Developer

Author

Commented:
Well, I may not like the answer but that is, indeed, the answer. It's just not possible. I'll find another way.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Did you try the solution I have posted?
Russ SuterSenior Software Developer

Author

Commented:
Sorry, I didn't see it. It actually worked perfectly. Thanks! :)
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Dude... you gave me a little heart attack... :P.. Glad it worked for you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial