[英]Weird issues with non-english characters
我有自動運行netsh的代碼來檢查防火牆規則:
var process = new Process
{
StartInfo =
{
FileName = fileName,
Arguments = arguments,
LoadUserProfile = true,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
process.OutputDataReceived += (sendingProcess, outLine) => outputText.AppendLine(outLine.Data);
process.ErrorDataReceived += (sendingProcess, errorLine) => errorText.AppendLine(errorLine.Data);
output = outputText.ToString();
errorOutput = errorText.ToString();
//parse both outputs...
作為文件名,我有以下命令:
C:\Windows\System32\netsh.exe
以下是參數:
advfirewall firewall show rule name="rulename" dir=in
它通常可以正常工作,當輸出包含非英語字符時,我會遇到問題。 它們全部替換為“?”,所以我認為這是編碼問題,但看起來不是。
當我在cmd中手動運行相同的命令時,我得到的結果非常相同。 但是,如果我將字體從默認字體更改為consolas,則它會很好地顯示非英語輸出。 我該如何在代碼中應用相同的修復程序,甚至可以完全做到這一點? 我搜索了一段時間,卻一無所獲,這表明我在這里可能做錯了什么。
同樣,編碼似乎不是問題,因為cmd和powershell在正確更改字體后都會顯示輸出。 我嘗試將StartInfo.StandardOutputEncoding
設置為cmd中使用的相同編碼,但沒有任何變化-我仍然在輸出中得到問號。 我想念什么?
編輯:嘗試將StartInfo.StandardOutputEncoding設置為不同的值,包括cp866,koi8-r和Windows-1251-結果保持不變,仍然出現問號。
我正在嘗試在Windows 2008 R2上以英語作為顯示語言。
如果規則名稱包含西里爾字母,那么您將需要使用將顯示它們的輸出控制台或編輯器來讀取它們。
在PowerShell中,使用以下命令並在寫字板或Word中編輯輸出。
(New-object -comObject HNetCfg.FwPolicy2).Rules | ForEach-Object { $_.Name } >rules.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.