我想让我的应用程序在桌面上创建一个快捷方式。原因是因为我的应用程序有一些这样的驴外部dll和其他依赖关系,我更喜欢它在一个文件夹中,只是在我的桌面上有一个快捷方式,而不是在我的桌面上的所有文件或包含所有内容的文件夹。创建具有依赖关系的应用程序的快捷方式
这是我第一次尝试这个。我相信这很简单,如果我的问题有点不好意思,那真的很抱歉。 我简单的代码看起来是这样的:
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();
}
}
但是我的应用程序不会有任何效果。只要它到达部件,它将需要依赖关系来继续它将崩溃。更具体地说,当我使用通过DLLImport
导入的方法(如bass.dll方法)时,它会简单地崩溃。当然,我的输出中看不到任何东西,因为这个快捷方式是一个编译好的.exe文件。
所以我的问题是这样的;我如何创建我的应用程序的完整快捷方式?
编辑: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;
}
的更新例如现在我相信这就是问题所在。但我不确定。外部dll(bass.dll,Newtonsoft.Json.dll和Spotify.dll)将Copy to output directory
设置为Copy always
并将它们复制。 .exe运行良好,创建并快速发送到桌面的快捷方式也可以正常运行。
编辑:更新2费德里科的例子 现在,这将工作:
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();
}
但是这里有一个小故障。 我的代码
public string mp3Path = Directory.GetCurrentDirectory() + "\\mp3Directory";
if (!System.IO.File.Exists(mp3Path))
{
Directory.CreateDirectory(mp3Path);
}
这部分将其更改为AppDomain.CurrentDomain.BaseDirectory
会很容易地解决它。
目前尚不清楚问题所在。那些依赖关系在哪里?你如何加载它们? – Evk
他们是外部dll。我通过DLLImport调用它们。有些位于应用程序位置,而其他位于同一位置的子文件夹中。 – user2530266
@ user2530266我认为,如果您手动创建快捷方式并尝试运行它,则会产生相同的行为。 – Valentin