2016-04-01 26 views
3

我想让我的应用程序在桌面上创建一个快捷方式。原因是因为我的应用程序有一些这样的驴外部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会很容易地解决它。

+0

目前尚不清楚问题所在。那些依赖关系在哪里?你如何加载它们? – Evk

+0

他们是外部dll。我通过DLLImport调用它们。有些位于应用程序位置,而其他位于同一位置的子文件夹中。 – user2530266

+0

@ user2530266我认为,如果您手动创建快捷方式并尝试运行它,则会产生相同的行为。 – Valentin

回答

2

尝试建立使用本地COM API的Windows快捷方式,在这个答案解释:https://stackoverflow.com/a/4909475/3670737

起初,项目>添加引用> COM> Windows脚本宿主对象模型。

using IWshRuntimeLibrary; 

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

[编辑1]后Directory.GetCurrentDirectory()问题更新时间:

documentationThe current directory is distinct from the original directory, which is the one from which the process was started.

你预期的目录由Directory.GetCurrentDirectory()返回的一个不同,因为你实际上是从一个不同的启动过程目录时使用快捷方式(您的桌面)。 Directory.GetCurrentDirectory()返回当前工作工作目录,如果您通过打开.exe文件启动它,它与您的程序集是相同的,但如果从.lnk文件启动该目录,则是该快捷方式目录。

在您的情况我会使用Assembly.GetExecutingAssembly().Location(检查这个答案的详细信息:https://stackoverflow.com/a/837501/3670737

+0

这是一个很好的解决方案。但是有一个小故障。我更新了我的第一个问题,如果你想看看 – user2530266

+0

在上次更新后编辑了答案。 –

+0

正如我所说,我当然可以调试这个问题。这就是为什么你的答案已被标记为正确的;)谢谢 – user2530266

1

如果我理解正确的话,这听起来像你可能会更好只是让你的应用程序的安装。你可以通过Visual Studio来做到这一点,它几乎可以为你自动完成这个过程。此外,您可能可以尝试使您的程序成为依赖关系的ClickOnce应用程序。我会亲自采取后者的选择。

我意识到这不是一个特别彻底的答案,但我觉得这个问题有点不清楚。

+0

说实话,我想过。不过,我现在要避开它。 – user2530266

相关问题