2008-10-24 83 views
67

如何在C#中或使用.NET框架創建應用程序快捷方式(.lnk文件)?在目錄中創建應用程序快捷方式

其結果將是一個.lnk文件到指定的應用程序或URL。

+0

這可能是有用的:http://www.codeproject.com/Articles/3905/Creating-Shell-Links-Shortcuts-in-NET-Programs-Usi – 2015-02-09 18:56:00

回答

61

這不是那麼簡單,我會很喜歡,但有一個很大的一流的呼叫ShellLink.csvbAccelerator

該代碼使用互操作,但不依賴於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); 
    } 
} 
+0

任何人都試圖Vista上的ShellLink?看起來代碼是在2003年編寫的。 – blak3r 2009-06-24 01:25:03

+0

它在Windows Server 2008 Standard 64bit SP2上工作。我認爲沒有理由不會在Vista上。 – 2011-01-07 19:49:19

+9

適用於Windows 7甚至64位應用程序:) – fparadis2 2011-07-19 14:25:24

14

,我發現這樣的事情:

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(); 
    } 
} 

原始代碼在sorrowman's article "url-link-to-desktop"

46

尼斯和清潔。 (.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); 
} 
1

Donwload IWshRuntimeLibrary

您還需要導入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(); 

} 
1

調查我發現了所以我看中了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)。

1

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(); 

Credit goes to Jim Hollenhorst

相關問題