none
Creating lnk shortcuts that has different names than the original file? RRS feed

  • Question

  • What im trying to do here is:

    Getting a name for the shortcut file.
    Then getting the path of the original file and the files name and extension itself.
    Then saving the shortcut as the name i gave to it.

    (using IWshRuntimeLibrary)

    				string scLoc = AppDomain.CurrentDomain.BaseDirectory + @"\shortcuts";
    				string typed = null;
    				while (true)
                                    {
                                        string regName = null;
                                        Console.WriteLine();
                                        inputedKey = Console.ReadKey(true);
                                        if (char.ToLower(inputedKey.KeyChar) == 'l')
                                        {
                                            Console.WriteLine("\nPlease choose the name of the registary:\n");
                                            regName = Console.ReadLine();
                                            regName = regName + ".lnk";
                                            Console.WriteLine("\nPlease enter the original file name and path as like this example \"X:\\Folder\\Program.ext\":\n");
                                            typed = Console.ReadLine();
                                            if (System.IO.File.Exists(typed) == false)
                                            {
                                                Console.WriteLine("File does not exist");
                                                continue;
                                            }
                                            else
                                            {
                                                WshShell shell = new WshShell();
                                                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(typed);
                                                shortcut.TargetPath = scLoc;
                                                Console.WriteLine("\nDone!");
                                                Console.ReadKey(true);
    					    break;
    //that part changed to this
                                                WshShell shell = new WshShell();
                                                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(scLoc + regName);
                                                shortcut.TargetPath = typed;
                                                shortcut.Save();
                                                Console.WriteLine("\nDone!");
                                                Console.ReadKey(true);
                                                break;
                                            }
                                        }
                                    }
                                    break;
    Whats the problem and how can i fix it?

    • Edited by xanrer Wednesday, January 16, 2019 1:18 AM fix
    Sunday, January 13, 2019 10:00 PM

Answers

  • Hi xanrer,

    I have downloaded the sample, any update, I will post here.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Fixed! The mistake was this line (regName = regName + ".lnk";)
    I changed it to this (regName =  "//" + regName + ".lnk";)
    Im so sorry for all the trouble i caused.
    Thursday, January 24, 2019 10:47 PM

All replies

  • Seems your code has missing parts e.g. no if and break is not where it would expected to be.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, January 13, 2019 11:52 PM
    Moderator
  • Hi xanrer,

    Please change the following line of code and check if it works for you.

    WshShell shell = new WshShell();
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(regName);
    shortcut.TargetPath = scLoc;
    Console.WriteLine("\nDone!");
    Console.ReadKey(true);
    break;

    And the following simple sample which worked on my side, which create a notepad shortcut on desktop.

    object shDesktop = (object)"Desktop";
    WshShell shell = new WshShell();
    string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Test.lnk";
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
    shortcut.Description = "New shortcut for a Notepad";
    shortcut.Hotkey = "Ctrl+Shift+N";
    shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
    shortcut.Save();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 15, 2019 8:12 AM
    Moderator
  • Seems your code has missing parts e.g. no if and break is not where it would expected to be.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sorry for that, i was trimming down the unimportant parts when i was writing this question.
    Tuesday, January 15, 2019 10:22 PM
  • Hi xanrer,

    Please change the following line of code and check if it works for you.

    WshShell shell = new WshShell();
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(regName);
    shortcut.TargetPath = scLoc;
    Console.WriteLine("\nDone!");
    Console.ReadKey(true);
    break;

    And the following simple sample which worked on my side, which create a notepad shortcut on desktop.

    object shDesktop = (object)"Desktop";
    WshShell shell = new WshShell();
    string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Test.lnk";
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
    shortcut.Description = "New shortcut for a Notepad";
    shortcut.Hotkey = "Ctrl+Shift+N";
    shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
    shortcut.Save();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks! i fixed that line added the save() and fixed another mistake, here is the current one:
    WshShell shell = new WshShell();
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(scLoc + regName);
    shortcut.TargetPath = typed;
    shortcut.Save();
    Console.WriteLine("\nDone!");
    Console.ReadKey(true);
    However its still not working :/
    Wednesday, January 16, 2019 1:16 AM
  • Hi xanrer,

    Do you encounter any exception, could you please share a complete sample via OneDrive to reproduce it.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 16, 2019 6:35 AM
    Moderator
  • Hi xanrer,

    Do you encounter any exception, could you please share a complete sample via OneDrive to reproduce it.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    I don't get any error or exception from it but its just not working, here is the link: https://1drv.ms/f/s!AgTkCi7A8MClgQfNdrPgC_nIXMFw
    Friday, January 18, 2019 4:06 PM
  • Hi xanrer,

    Do you encounter any exception, could you please share a complete sample via OneDrive to reproduce it.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Zhanglong? How did the analyzing go?
    Saturday, January 19, 2019 8:21 PM
  • Hi xanrer,

    I have downloaded the sample, any update, I will post here.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 21, 2019 5:16 AM
    Moderator
  • Hi xanrer,

    I have downloaded the sample, any update, I will post here.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Fixed! The mistake was this line (regName = regName + ".lnk";)
    I changed it to this (regName =  "//" + regName + ".lnk";)
    Im so sorry for all the trouble i caused.
    Thursday, January 24, 2019 10:47 PM