Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, April 14, 2013 8:50 PM
Hi,
I understand this will code a shortcut in Visual Basic. The code was copied. Is there anyone who could translate this code into C#? The objective being to have code to put a shortcut on the Desktop. I don't see an icon being included. Is there better code than this?
Many thanks,
Zach.
Imports IWshRuntimeLibrary
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim WshShell As WshShellClass = New WshShellClass
Dim MyShortcut As IWshRuntimeLibrary.IWshShortcut
' The shortcut will be created on the desktop
Dim DesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
MyShortcut = CType(WshShell.CreateShortcut(DesktopFolder & "\MyShortcut.lnk"), IWshRuntimeLibrary.IWshShortcut)
MyShortcut.TargetPath = Application.StartupPath & "\YourApp.exe" 'Specify target app full path
MyShortcut.Save()
End Sub
All replies (14)
Sunday, April 14, 2013 9:32 PM ✅Answered
@DirkStrauss: You should always use an API (whenever there is one), instead of duplicating/emulating the logic. In a future version of Windows, the implementation of shortcuts might change so that your version of the logic will no longer work, but the API will probably still exist and give you what you want.
C# version of the OP's VB.NET code (which is good in the sense that it uses an API):
private void Button1_Click(object sender, EventArgs e)
{
var wshShell = new WshShell();
IWshRuntimeLibrary.IWshShortcut myShortcut;
// The shortcut will be created on the desktop
var desktopFolder = Environment.GetFolderPath(
Environment.SpecialFolder.DesktopDirectory);
myShortcut = (IWshRuntimeLibrary.IWshShortcut)
wshShell.CreateShortcut(desktopFolder +
@"\YourApp.exe"); // Specify target app full path
myShortcut.Save();
}
That code is incorrect, though! It will compile, but will raise exceptions when executed. To make it work, replace the lines starting with "myShortcut" with the following:
var shortcutPath = desktopFolder + @"\YourApp.lnk";
myShortcut = (IWshRuntimeLibrary.IWshShortcut)
wshShell.CreateShortcut(shortcutPath);
myShortcut.TargetPath =
"YourApp.exe"; // Specify target app full path
myShortcut.Save();
Edit: I forgot to mention that you need to add a reference the COM library "Windows Script Host Object Model" in your C# project to make the code work.
Also, about icons: To specify an icon, set the IconLocation property of myShortcut to "iconFile, index", where iconFile is the path to the file that contains the icon and index is the index of the icon within that file, e.g. "notepad.exe, 0".
For more information, see CreateShortcut, WshShortcut Object, IconLocation, etc. in the Windows Script Host reference.
Marcus Björklund
Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!
Tuesday, April 16, 2013 5:41 PM ✅Answered
It transpires that there is absolutely nothing wrong wth this code:
private void CreateShortcut()
{
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop)+@"\xxxxx.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "xxxxx";
shortcut.IconLocation = @"c:\Icoo\Zzzzzz.ico";
shortcut.TargetPath = @"c:\yyyyyyyyyy\xxxxx.exe";
shortcut.Save();
}
Zach.
Sunday, April 14, 2013 8:56 PM
Hello
Have a look at this link: http://www.sorrowman.org/c-sharp-programmer/url-link-to-desktop.html
This could also help: http://stackoverflow.com/questions/4897655/create-shortcut-on-desktop-c-sharp
Good luck!
When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community. Happy programming!
Sunday, April 14, 2013 9:47 PM
Hi Marcus,
I am impressed by the detail of your response. I will try it and see where it leads me. Afterwards I would like to get back to you about the results. Hopefully some time during the coming days.
Many thanks,
Zach.
Sunday, April 14, 2013 10:23 PM
Hi Marcus,
I am impressed by the detail of your response. I will try it and see where it leads me. Afterwards I would like to get back to you about the results. Hopefully some time during the coming days.
Many thanks,
Zach.
Hi Zach,
Thanks. I have updated my answer with some details that I left out before. I hope you'll find it even more useful now.
Marcus Björklund
Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!
Monday, April 15, 2013 4:03 PM
Hi Marcus,
I am starting to make use of your code and have a query, mamely:
"Edit: I forgot to mention that you need to add a reference the COM library "Windows Script Host Object Model"
Sorry, I don't know how to do this. Would you mind telling me?
Many thanks,
Zach.
Monday, April 15, 2013 4:40 PM
Hi Marcus,
I am starting to make use of your code and have a query, mamely:
"Edit: I forgot to mention that you need to add a reference the COM library "Windows Script Host Object Model"
Sorry, I don't know how to do this. Would you mind telling me?
Many thanks,
Zach.
Hi Zach,
No problem :-)
From the Visual Studio menus, select Project > Add Reference...
In the dialog that opens, select the "COM" tab/section and locate "Windows Script Host Object Model" in the list. In case you are using VS2012 you need to put a checkmark on the list item before clicking the OK button.
The reference will show up as "IWshRuntimeLibrary" in the Solution Explorer.
Marcus Björklund
Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!
Monday, April 15, 2013 6:07 PM
Hi Marcus,
Sorry, when compiling I get errors on
"var" and "new WshShell()"
Zach.
Monday, April 15, 2013 8:59 PM
Hi Marcus,
Sorry, when compiling I get errors on
"var" and "new WshShell()"
Zach.
I forgot to mention that you need to add the following "using" statement at the beginning of your code file:
using IWshRuntimeLibrary;
Sorry about that. Btw, did you know that you can make Visual Studio insert the "using" statement for you? If you place the text cursor on "new WshShell()", you'll notice that a small blue box (a so-called smart tag) appears. If you hover over it with the mouse pointer, it will expand to a little drop-drown menu in which you can select to add the "using" statement. A quicker way to open the smart tag menu is to use the keyboard shortcut Ctrl+. or Shift+Alt+F10.
I find it strange that you got a compile error on the "var" keyword. What version of Visual Studio and .NET Framework are you using? The "var" keyword has been around since C# 3.0 (VS2008 / .NET 3.5). If you are using an earlier version, you will have to specify all types explicitly by replacing "var wshShell" with "WshShell wshShell" and "var desktopFolder" with "string desktopFolder".
Marcus Björklund
Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!
Tuesday, April 16, 2013 4:42 AM
Hi Marcus,
Having added: "using IWshRuntimeLibrary;"
I receive an error: "Could not load the file or assembly "Interop.IWsh ...". The system could not find the file specified"
You askeed me which VS I am using: it is VS2005.
Regards,
Zach.
Tuesday, April 16, 2013 1:05 PM
Marcus' code didn't work forme.
Re: http://stackoverflow.com/questions/4897655/create-shortcut-on-desktop-c-sharp
Based on this reference the codebelow worked on test.
After I incorporated the code in the application it no longer did.
I am unable to find out why and would appreciate some help.
Zach.
private void CreateShortcut()
{
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) +@"\xxxxx.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "xxxxx";
shortcut.IconLocation = @"c:\Icoo\Zzzzzz.ico";
shortcut.TargetPath = @"c:\yyyyyyyyyy\xxxxx.exe";
shortcut.Save();
}
Tuesday, April 16, 2013 3:41 PM
Things not to your liking / things that could be done better?
The following line looks a bit weird:
object shDesktop = (object)"Desktop";
I think it could be replaced by this:
string shDesktop = "Desktop";
Other than that, the code looks OK.
Marcus Björklund
Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!
Tuesday, April 16, 2013 3:50 PM
Marcus,
Your suggestion: string shDesktop = "Desktop"; No, that isn't the cause of the error. Idid try it to be sure. I am quite certain it has to do with the icon location. I say that because I have attempted to sort out which line in the code causes the error.
Zach.
Tuesday, April 16, 2013 4:05 PM
I am quite certain it has to do with the icon location. I say that because I have attempted to sort out which line in the code causes the error.
Since you are specifying a hard-coded absolute path (like c:\Icoo\Zzzzzz.ico) for IconLocation, you need to make sure that the .ico can be found at this location on all computers where you run your program. The same goes for the TargetPath (.exe file).
To allow these paths to vary, you need some way to figure them out, perhaps in combination with some input from the user. The System.IO.Path class has some useful methods for constructing paths, such as GetFullPath, GetDirectoryName, Combine and others. (If you need more help regarding how to deal with paths, please post a new question.)
Marcus Björklund
Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!