Powershell是否存在语言无关的文件系统对象动词?

时间:2018-12-12 08:09:12

标签: windows powershell

当我想使用Powershell 固定文件夹到文件资源管理器的 Quick Access 部分时,我会这样做

$folderPath = "C:\Windows"
$shell = New-Object -ComObject shell.application 
$folder = $Shell.Namespace("$folderPath").Self
$verb = $folder.Verbs() | Where-Object {$_.Name.replace('&', '') -match 'Pin to Quick access'}
if ($verb) {
    $verb.DoIt()
}

由于动词名称上的匹配,如果该功能在非英语的Windows上运行,并且该函数动词名称取决于语言,则该功能将失败。

我还发现此代码似乎调用了键之类的东西,而不是动词名称,但是如果OS语言不是英语,它也会失败。

$folder.InvokeVerb("pintohome")

在任何语言版本的Windows中,是否有任何语言中立的动词“键” ?如果是,我在哪里可以找到它们?

1 个答案:

答案 0 :(得分:3)

简单的回答是“否”。

但是,如果您要深入研究Windows MUI资源并在其中找到您所用语言的动词,则可以解决该问题。

不幸的是,此解决方法有其自身的缺点:对于每个Windows版本,这些资源都是不同的。您可以在此处查看有关此解决方案http://alexweinberger.com/main/pinning-network-program-taskbar-programmatically-windows-10/

的详细信息

为完整起见,从此处复制粘贴代码(仅适用于Windows 7):

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
internal static extern IntPtr LoadLibrary(string lpLibFileName);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
internal static extern int LoadString(IntPtr hInstance, uint wID, StringBuilder lpBuffer, int nBufferMax);

public static bool PinUnpinTaskbar(string filePath, bool pin)
{
    if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);
    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
    //uncomment the following line to pin to start instead
    //actionIndex = pin ? 51201 : 51394;
    StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
    IntPtr hShell32 = LoadLibrary("Shell32.dll");
    LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
    string localizedVerb = szPinToStartLocalized.ToString();

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

    // create the shell application object
    dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
    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 (verb.Name.Equals(localizedVerb))
        {
            verb.DoIt();
            return true;
        }
    }
    return false;
}