I am trying to get my app create a shortcut on the desktop. The reason is because my app has some dependencies such ass external dlls and other and I prefer to have it in a folder and simply have a shortcut on my desktop rather than having all the files on my desktop or a folder containing everything.
It's the first time I am trying this. I believed it was simple and indeed it is so sorry if my question is a bit nooby. My simple code looks like this:
string checkDesktopDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
if (!File.Exists(checkDesktopDir + "\\My app.url"))
{
ShortCutWithDependencies("My app");
}
private void ShortCutWithDependencies(string sAppName)
{
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
using (StreamWriter writer = new StreamWriter(deskDir + "\\" + sAppName + ".url"))
{
string app = Assembly.GetExecutingAssembly().Location;
MessageBox.Show(app);
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=" + app);
writer.WriteLine("IconIndex=0");
string icon = app.Replace('\\', '/');
writer.WriteLine("IconFile=" + icon);
writer.Flush();
}
}
However my app will never work. As soon as it get to the part it will need the dependencies to continue it will crash. More specific, when I use the methods imported via DLLImport
such as bass.dll methods, it will simply crash. Of course I can't see anything in my output since the shortcut is a compiled .exe.
So my question is this; how do I create a full shortcut of my application?
Edit: updating example of dllimport
[DllImport("bass.dll")]
public static extern bool BASS_Start();
[DllImport("bass.dll")]
public static extern bool BASS_Init(int device, uint freq, uint flag,
IntPtr hParent, uint guid);
if (mp3ToPlay != string.Empty)
{
BASS_Start();
BASS_Init(-1, 44100, 0, IntPtr.Zero, 0);
BassHandle = BASS_StreamCreateFile(false, mp3ToPlay, 0, 0, 0x20000);
BASS_ChannelPlay(BassHandle, false);
PlayBackLength = BASS_ChannelGetLength(BassHandle, 0);
PlayBack = true;
}
Now I believe this is the problem. I am not sure though. The external dlls (bass.dll, Newtonsoft.Json.dll and Spotify.dll) have Copy to output directory
set as Copy always
and they are copied. The .exe will run fine and a shortcut created and sent to desktop manually will run fine as well.
Edit: Update 2 with Federico's example Now this will work:
private void CreateShortcut()
{
object shDesktop = "Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\iBlock.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for a iBlock";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\iBlock\iBlock.exe";
shortcut.Save();
}
However there is a glitch here. This part of my code
public string mp3Path = Directory.GetCurrentDirectory() + "\\mp3Directory";
if (!System.IO.File.Exists(mp3Path))
{
Directory.CreateDirectory(mp3Path);
}
Changing it to AppDomain.CurrentDomain.BaseDirectory
will easily fix it.