Error while creating shortcut button C#

2

I'm trying to have a button that once you press it, it will create a shortcut.

Any time I press the button I am getting an error

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred

Additional information: Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))

Can anyone see why this is happening?

public static void CreateShortcut(string shortcutName)
    {
        WshShell wsh = new WshShell();
        string fileName = savDir + "\\" + ProductName + ".ink";

        IWshShortcut shortcut = (IWshShortcut)wsh.CreateShortcut(fileName);
        shortcut.Targetpath = Application.ExecutablePath;
        shortcut.Save();

    }


    private void button2_Click(object sender, EventArgs e)
    {
        string folder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
       CreateShortcut("folder");
    }
c#
asked on Stack Overflow Jul 19, 2016 by Web test

2 Answers

2

Your call to CreateShortcut looks wrong:

CreateShortcut("folder");

should be (no quotes):

CreateShortcut(folder);
answered on Stack Overflow Jul 19, 2016 by CodeNaked
0

I also agree that you would want to change

"folder" -> folder

However, I got the same exception. I think the issue is your extension. Try:

".lnk" instead of ".ink"
answered on Stack Overflow May 7, 2019 by techcase

User contributions licensed under CC BY-SA 3.0