gpt4 book ai didi

c# - 使用 C# 将 *.lnk 文件固定到 Windows 7 任务栏

转载 作者:可可西里 更新时间:2023-11-01 08:41:37 26 4
gpt4 key购买 nike

即使在 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 Framework?有什么想法吗?

最佳答案

简单...

    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,那么您将不得不使用反射。但这会使代码更难看。

关于c# - 使用 C# 将 *.lnk 文件固定到 Windows 7 任务栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6872103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com