2016-09-07 21 views
0

我不知道如何添加變音符快捷方式的文件名和目標路徑。如何讓快捷方式中的變音符號不變?

$ws_shell = New-Object -COMObject WScript.Shell 
$shortcut = $ws_shell.CreateShortcut("Yū-Sibu.lnk") 
$shortcut.TargetPath = "Yū-Sibu" 

此代碼輸出一個路徑'Yu-Sibu.lnk'的快捷方式,並以'Yu-Sibu'爲目標。

即使Write-Host $shortcut.TargetPath返回'Yu-Sibu'。

+0

看到這一點:http://stackoverflow.com/questions/1056692/how-to-encode-unicode-character-codes-in-a-powershell-string-字面 – Avshalom

+0

即使在今天,我強烈建議不要在文件名中使用非ASCII字符。 – vonPryz

+0

十億人使用非ASCII文件名,因此完全沒問題。至於這個問題,你正在使用一種老式的恐龍廢棄的WScript。查看C#創建一個unicode快捷方式lnk的例子:[用Unicode字符創建快捷方式](https://stackoverflow.com/a/13542488) – wOxxOm

回答

0

WScript是古老的,不能正確處理unicode,所以我們將使用新的ShellLink interface。由於它只能修改現有的lnk文件,所以我們將首先用WScript創建一個臨時的空lnk。

function Create-Lnk(
    [Parameter(Mandatory=$true)] 
    [ValidateScript({Test-Path -IsValid -Literal $_})] 
    [string]$lnk, 

    [Parameter(Mandatory=$true)] 
    [ValidateScript({Test-Path -Literal $_})] 
    [string]$target, 

    [string]$arguments = '', 
    [string]$description = '', 
    [string]$workingDir = '', 

    [ValidateSet('normal', 'minimized', 'maximized')] 
    [string]$windowState 
) { 
    $tmpName = [IO.Path]::GetRandomFileName() + '.lnk' 
    $tmpFolder = $env:TEMP 
    $tmpFullPath = Join-Path $tmpFolder $tmpName 

    $ws_shell = New-Object -com WScript.Shell 
    $shortcut = $ws_shell.CreateShortcut($tmpFullPath) 
    $shortcut.Save() 

    $shellApp = New-Object -com Shell.Application 
    $shellLink = $shellApp.NameSpace($tmpFolder).ParseName($tmpName).GetLink() 

    $shellLink.Path = $target 
    $shellLink.Arguments = $arguments 
    $shellLink.Description = $description 
    $shellLink.WorkingDirectory = $workingDir 
    $shellLink.ShowCommand = switch($windowState){'minimized'{2} 'maximized'{3} default{1}} 
    $shellLink.Save() 

    move -literal $tmpFullPath $lnk -force 
} 

實施例:

Create-Lnk -lnk (Join-Path ([Environment]::GetFolderPath('Desktop')) 'пишижиши.lnk') ` 
      -target 'D:\lost in translation\なんでやねん!.txt' ` 
      -description '¿Por qué dices eso?'