2017-04-04 55 views
0

這裏是我的代碼:如何在同一文件夾中創建我的項目.exe的快捷方式.exe位於?

 private void button_Click_1(object sender, RoutedEventArgs e) 
    { 
     string link = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
      + System.IO.Path.DirectorySeparatorChar + System.Windows.Forms.Application.ProductName + ".lnk"; 
     var shell = new WshShell(); 
     var shortcut = shell.CreateShortcut(link) as IWshShortcut; 
     shortcut.TargetPath = System.Windows.Forms.Application.ExecutablePath; 
     shortcut.WorkingDirectory = System.Windows.Forms.Application.StartupPath; 
     //shortcut... 
     shortcut.Save(); 
    } 

此代碼在桌面上創建一個快捷方式......我想在我的exe文件相同的文件夾中創建一個快捷方式。我不能指出文件夾的路徑,因爲我不知道其他人會將其解包並運行它:(

回答

0

當然,它確實如此。這就是您在使用Environment.SpecialFolder.Desktop時所說的創建它的原因。你希望它出現在其他地方?

您需要使用您分配給shortcut.WorkingDirectory,而不是同一個位置。

private void button_Click_1(object sender, RoutedEventArgs e) 
{ 
    string link = System.Windows.Forms.Application.StartupPath + 
     System.IO.Path.DirectorySeparatorChar + 
     System.Windows.Forms.Application.ProductName + ".lnk"; 
    var shell = new WshShell(); 
    var shortcut = shell.CreateShortcut(link) as IWshShortcut; 
    shortcut.TargetPath = System.Windows.Forms.Application.ExecutablePath; 
    shortcut.WorkingDirectory = System.Windows.Forms.Application.StartupPath; 
    //shortcut... 
    shortcut.Save(); 
} 
相關問題