我正在使用powershell制作表单应用程序。我在使字符“æ”,“ø”,“å”正确显示在标签中时遇到问题。有关它们的显示方式,请参见下面的屏幕抓图。似乎存在某种类型的编码问题,由于Powershell将字符作为utf-16保留在内存中,所以我猜问题出在我正在使用的System.Windows.Forms.Label
上。
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Width = 800
$Form.Height = 500
$Form.BackColor = "#dff3ee" #"#00554b"
$Form.AutoSize = $false
$Form.StartPosition = "CenterScreen"
$Info= New-Object System.Windows.Forms.Label
$Info.Width = 250
$Info.Height = 70
$Info.AutoSize = $false
$Info.Location = New-Object System.Drawing.Point(20,20)
$Info.Font = "Arial Unicode MS, 9"
$Info.Text = "Why is this æ ø å displayed wrong"
$Form.Controls.AddRange(@($Info))
$Form.ShowDialog()
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
我无法解决此问题。但是我用PS2EXE将其打包在.exe中,然后正确显示了字符。由于它在生产中是正确的,所以我不再为此烦恼。
这是保存文件的工具的问题。如果使用Windows PowerShell ISE
,它将使用UTF-8 With BOM
保存文件(这意味着带有签名),但是当您使用VS CODE
时,默认设置是使用UTF-8
保存文件(这意味着没有BOM或没有签名) 。
要解决VS CODE中的问题:
UTF-8
,单击它