[英]Pin *.lnk file to Windows 7 Taskbar using C#
即使在 Windows 7 中以編程方式固定圖標似乎也是不允許的(就像這里說的: http : //msdn.microsoft.com/en-us/library/dd378460(v=VS.85 ) .aspx ),有一些通過使用一些 VB 腳本來執行此操作的方法。 有人在 C# 中找到了一種方法,如下所示:
private static void PinUnpinTaskBar(string filePath, bool pin)
{
if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);
// create the shell application object
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
string path = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
dynamic directory = shellApplication.NameSpace(path);
dynamic link = directory.ParseName(fileName);
dynamic verbs = link.Verbs();
for (int i = 0; i < verbs.Count(); i++)
{
dynamic 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;
}
可以看出,該代碼使用了 .NET Framework 4.0 功能。 我想問的問題是:這個函數可以轉換成同樣的東西,但只使用 3.5 框架嗎? 有任何想法嗎?
簡單的...
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;
}
請務必添加對“Microsoft Shell 控件和自動化”的 COM 引用。
如果您想保留使用 Activator.CreateInstance 的現有方法,這樣您就不必擁有額外的 COM 互操作 DLL,那么您必須使用反射。 但這會使代碼更加丑陋。
無論 Windows 用戶使用什么本地化:
int MAX_PATH = 255;
var actionIndex = pin ? 5386 : 5387; // 5386 is the DLL index for"Pin to Tas&kbar", ref. http://www.win7dll.info/shell32_dll.html
StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
IntPtr hShell32 = LoadLibrary("Shell32.dll");
LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
string localizedVerb = szPinToStartLocalized.ToString();
// create the shell application object
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
string path = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
dynamic directory = shellApplication.NameSpace(path);
dynamic link = directory.ParseName(fileName);
dynamic verbs = link.Verbs();
for (int i = 0; i < verbs.Count(); i++)
{
dynamic verb = verbs.Item(i);
if ((pin && verb.Name.Equals(localizedVerb)) || (!pin && verb.Name.Equals(localizedVerb)))
{
verb.DoIt();
break;
}
}
使用 Fall Creators Update(內部版本 16299 或更高版本)更新在 Windows 10 上運行的 UWP 應用程序:
您可以以編程方式將您自己的應用程序固定到任務欄,就像您可以將您的應用程序固定到“開始”菜單一樣。 您可以檢查您的應用程序當前是否已固定,以及任務欄是否允許固定。
您可以直接訪問TaskbarManager
API,如MS docs 中所述。
我想用 WinForms 應用程序來做到這一點,但它似乎不會工作,因為我無法引用Windows.Foundation
API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.