回答
這不是那麼簡單,我會很喜歡,但有一個很大的一流的呼叫ShellLink.cs在 vbAccelerator
該代碼使用互操作,但不依賴於WSH。
使用這個類,創建快捷方式的代碼是:
private static void configStep_addShortcutToStartupGroup()
{
using (ShellLink shortcut = new ShellLink())
{
shortcut.Target = Application.ExecutablePath;
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
shortcut.Description = "My Shorcut Name Here";
shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
shortcut.Save(STARTUP_SHORTCUT_FILEPATH);
}
}
,我發現這樣的事情:
private void appShortcutToDesktop(string linkName)
{
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
{
string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=file:///" + app);
writer.WriteLine("IconIndex=0");
string icon = app.Replace('\\', '/');
writer.WriteLine("IconFile=" + icon);
writer.Flush();
}
}
尼斯和清潔。 (.NET 4.0)
Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object
dynamic shell = Activator.CreateInstance(t);
try{
var lnk = shell.CreateShortcut("sc.lnk");
try{
lnk.TargetPath = @"C:\something";
lnk.IconLocation = "shell32.dll, 1";
lnk.Save();
}finally{
Marshal.FinalReleaseComObject(lnk);
}
}finally{
Marshal.FinalReleaseComObject(shell);
}
就是這樣,不需要額外的代碼。 CreateShortcut甚至可以從文件加載快捷方式,因此像TargetPath等屬性返回現有信息。 Shortcut object properties。
對於不支持.NET的動態類型的版本,這種方法也是可行的。 (.NET 3.5)
Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object
object shell = Activator.CreateInstance(t);
try{
object lnk = t.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[]{"sc.lnk"});
try{
t.InvokeMember("TargetPath", BindingFlags.SetProperty, null, lnk, new object[]{@"C:\whatever"});
t.InvokeMember("IconLocation", BindingFlags.SetProperty, null, lnk, new object[]{"shell32.dll, 5"});
t.InvokeMember("Save", BindingFlags.InvokeMethod, null, lnk, null);
}finally{
Marshal.FinalReleaseComObject(lnk);
}
}finally{
Marshal.FinalReleaseComObject(shell);
}
您還需要導入COM庫IWshRuntimeLibrary
的。右鍵單擊你的項目 - >添加引用 - > COM - > IWshRuntimeLibrary - >添加,然後使用下面的代碼片段。
private void createShortcutOnDesktop(String executablePath)
{
// Create a new instance of WshShellClass
WshShell lib = new WshShellClass();
// Create the shortcut
IWshRuntimeLibrary.IWshShortcut MyShortcut;
// Choose the path for the shortcut
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
MyShortcut = (IWshRuntimeLibrary.IWshShortcut)lib.CreateShortcut(@deskDir+"\\AZ.lnk");
// Where the shortcut should point to
//MyShortcut.TargetPath = Application.ExecutablePath;
MyShortcut.TargetPath = @executablePath;
// Description for the shortcut
MyShortcut.Description = "Launch AZ Client";
StreamWriter writer = new StreamWriter(@"D:\AZ\logo.ico");
Properties.Resources.system.Save(writer.BaseStream);
writer.Flush();
writer.Close();
// Location for the shortcut's icon
MyShortcut.IconLocation = @"D:\AZ\logo.ico";
// Create the shortcut at the given path
MyShortcut.Save();
}
調查我發現了所以我看中了ShellLink所有可能性之後:
//Create new shortcut
using (var shellShortcut = new ShellShortcut(newShortcutPath)
{
Path = path
WorkingDirectory = workingDir,
Arguments = args,
IconPath = iconPath,
IconIndex = iconIndex,
Description = description,
})
{
shellShortcut.Save();
}
//Read existing shortcut
using (var shellShortcut = new ShellShortcut(existingShortcut))
{
path = shellShortcut.Path;
args = shellShortcut.Arguments;
workingDir = shellShortcut.WorkingDirectory;
...
}
除了被簡單有效,筆者(馬蒂亞斯舍格倫,MS MVP)是某種COM的/ PInvoke/Interop guru,並仔細閱讀他的代碼,我相信它比其他代碼更強大。
應當提及的是快捷方式文件也可以通過幾個命令行實用程序(這反過來又可以從C#容易地調用/。NET)來創建。我從來沒有嘗試過其中任何一個,但我會從NirCmd開始(NirSoft有像SysInternals一樣的質量工具)。
不幸的是NirCmd不能解析快捷文件(只創建它們),但爲此目的TZWorks lp似乎有能力。它甚至可以將它的輸出格式化爲csv。 lnk-parser看起來不錯(它可以輸出HTML和CSV)。
與IllidanS4's answer類似,使用Windows Script Host證明是最簡單的解決方案(在Windows 8 64位上測試)。
但是,不是通過代碼手動導入COM類型,而是將COM類型庫作爲參考添加更容易。選擇References->Add Reference...
,COM->Type Libraries
並找到並添加「Windows腳本宿主對象模型」。
這將導入命名空間IWshRuntimeLibrary
,從中你可以訪問:
WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkPathName);
link.TargetPath=TargetPathName;
link.Save();
- 1. 創建應用程序快捷方式
- 2. 如何重新創建webstart應用程序的快捷方式?
- 3. 爲Node Webkit應用程序創建桌面快捷方式
- 4. 如何創建Java桌面應用程序的快捷方式?
- 5. 爲應用程序創建快捷方式短語
- 6. .net .vdproj - 應用程序快捷方式
- 7. 應用程序快捷方式
- 8. 離線應用程序快捷方式
- 9. C#創建快捷方式
- 10. 在HomeScreen中創建快捷方式
- 11. 在winforms中創建快捷方式VS2017
- 12. 在Windows中創建自定義命令(不是應用程序)快捷方式
- 13. 創建批處理和快捷方式的快捷方式
- 14. 如何在Java中使用windows shell32.dll創建目錄快捷方式?
- 15. 如何使用WIX創建卸載程序快捷方式
- 16. 在Windows 8上創建非城域應用程序的快捷方式?
- 17. 在桌面上創建Windows 10應用程序快捷方式vbsscript
- 18. 以編程方式創建組合桌面快捷鍵「快捷方式」
- 19. 使用.Net創建快捷方式
- 20. 在Android上創建快捷方式
- 21. 如何以編程方式使用Win32創建快捷方式
- 22. android在視圖中創建快捷應用程序視圖
- 23. 在C中創建單個項目列表的快捷方式#
- 24. 如何在應用程序項目中以編程方式創建目錄?
- 25. 如何在android中以編程方式更改應用程序圖標而不創建快捷方式?
- 26. 在Eclipse中添加應用程序啓動快捷方式?
- 27. 如何在應用程序中實現快捷方式
- 28. Linux更改目錄快捷方式
- 29. 如何在android中以編程方式添加應用程序快捷方式
- 30. 如何在應用程序安裝後自動創建android應用程序的快捷方式
這可能是有用的:http://www.codeproject.com/Articles/3905/Creating-Shell-Links-Shortcuts-in-NET-Programs-Usi – 2015-02-09 18:56:00