2012-09-01 49 views

回答

0
private static void PinUnpinTaskBar(string filePath, bool pin) { 
    if (!File.Exists(filePath)) throw new FileNotFoundException(filePath); 

    // create the shell application object 
    Shell shellApplication = new ShellClass(); 

    string path = Path.GetDirectoryName(filePath); 
    string fileName = Path.GetFileName(filePath); 

    Folder directory = shellApplication.NameSpace(path); 
    FolderItem link = directory.ParseName(fileName); 

    FolderItemVerbs verbs = link.Verbs(); 
    for (int i = 0; i < verbs.Count; i++) { 
     FolderItemVerb verb = verbs.Item(i); 
     string verbName = verb.Name.Replace(@"&", string.Empty).ToLower(); 

     if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar"))) { 

      verb.DoIt(); 
     } 
    } 

    shellApplication = null; 
} 

一定要包括 「微軟殼牌控制及自動化」 參考

,並說感謝@詹姆斯約翰斯頓 - 他original post

+0

我希望安裝程序固定應用程序,而不是應用程序本身。我應該在哪裏輸入此代碼? :@ – TYeeTY

+0

它會將.lnk(shorcut)固定到任務欄,如果您想要安裝程序執行此操作,請運行腳本 http://blogs.technet.com/b/deploymentguys/archive/2009/04/ 08/pin-items-to-the-start-menu-or-windows-7-taskbar-via-script.aspx –

2

Vb.net用於從任務欄和開始菜單固定/取消固定的代碼片段。 (框架3.5)

Dim shellApplication As Shell = New ShellClass() 

Dim directoryName As String = Path.GetDirectoryName(filePath) 
Dim fileName As String = Path.GetFileName(filePath) 

Dim directory As Shell32.Folder = shellApplication.[NameSpace](directoryName) 
Dim link As FolderItem = directory.ParseName(fileName) 

Dim verbs As FolderItemVerbs = link.Verbs() 
For i As Integer = 0 To verbs.Count - 1 
    Dim verb As FolderItemVerb = verbs.Item(i) 
    Dim verbName As String = verb.Name.Replace("&", String.Empty) 
If (verbName.Equals("Pin to Start Menu")) Or (verbName.Equals("Unpin from Start Menu")) Then 

    verb.DoIt() 
    End If 
Next 
shellApplication = Nothing 

「文件路徑是要固定.exe文件/取消固定任務欄

釘扎/任務欄取消固定案例的替代‘固定到開始菜單’與「銷路徑任務欄」 和‘取消固定從開始菜單’,以‘從任務欄取消固定’

所有固定文件駐留在

C:\Users\%LoggedIn_User_Name%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned 

本規範工作的Windo ws7美國英語。

乾杯!

+0

我是C#程序員,但這不是問題。我想安裝程序爲我做這個,所以我應該在哪裏寫這些代碼? – TYeeTY

+0

如果您收到類似'Interop type'ActiveHomeScriptLib.ActiveHomeClass'的錯誤無法嵌入。改爲使用適用的接口。「您應該將shell32程序集的屬性」Embed Interop Types「設置爲」False「。 https://stackoverflow.com/questions/2483659/interop-type-cannot-be-embedded – PeterCo

相關問題